MATLAB Answers

Infinite for loop getting stuck. Creating a new column containing a variable.

1 view (last 30 days)
nskel
nskel on 26 Aug 2019
Commented: Andrei Bobrov on 30 Aug 2019
Hi, I am trying to generate a new column variable within a table which is conditional on 2 other values. The for loop I am using to generate it is getting stuck, however. It is generating the right values for me but just gets stuck and goes through the column over and over again. Any help would be appreciated:
data.signal = zeros(size(data.NAPMPMIIndex));
for indx=1:numel(data.NAPMPMIIndex)
if(data.NAPMPMIIndex(indx) > 50 & data.AboveMA(indx)> 0.5)
data.signal(indx) = 0.5
elseif(data.NAPMPMIIndex(indx) > 50 & data.AboveMA(indx)< 0.5)
data.signal(indx) = -0.5
elseif(data.NAPMPMIIndex(indx) < 50 & data.AboveMA(indx)> 0.5)
data.signal(indx) = 1
else
data.signal(indx) = -1;
end
end
  10 Comments
nskel
nskel on 29 Aug 2019
ah, thanks very much.
I'm quite new to this but that is very much a rookie error
:-)

Sign in to comment.

Accepted Answer

Andrei Bobrov
Andrei Bobrov on 27 Aug 2019
kk = [data.NAPMPMIIndex > 50, data.AboveMA > .5];
g = findgroups(kk(:,1),kk(:,2));
a = [-1;1;-.5;.5];
data.signal = a(g);
  3 Comments

Sign in to comment.

More Answers (0)

Tags

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!