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

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

What does
size(data.NAPMPMIIndex)
return? Your loop operates over all the elements of that array you created of that size, not just a column...perhaps it's just a lot more than you bargained for.
There's nothing in the above code snippet that is infinite--possibly numel(data.NAPMPMIIndex) is quite large, but it would finish eventually as long as you don't run out of memory.
There's other code we can't see obviously because otherwise have undefined variables but this isn't the problem.
The above assignments code be done w/o looping or explicit if...elseif by use of logical indexing.
>> size(data.NAPMPMIIndex)
ans =
714 1
I tried to change this to" numel(data.NAPMPMIIndex) " but the same problem occurs. The answer to this in this case is 714
Any chance we could run the code (ie, attach a mat file with all needed variables)?
Build the array then assign to the table...
signal = zeros(size(data.NAPMPMIIndex));
indx=(data.NAPMPMIIndex(indx)>50) & (data.AboveMA(indx)>0.5);
signal(indx) = 0.5;
indx=(data.NAPMPMIIndex(indx)>50) & (data.AboveMA(indx)<0.5);
signal(indx) = -0.5;
indx=(data.NAPMPMIIndex(indx)<50) & (data.AboveMA(indx)>0.5);
signal(indx) = 1;
indx=(data.NAPMPMIIndex(indx)<50) & (data.AboveMA(indx)<0.5);
signal(indx) = -1;
data.signal(indx) =signal;
NB: You have no assignment for the case of ==50 or ==0.5
The above could be shortened somewhat by use of logical relation and calculated result for the second condition to flip sign but just translated existing logic directly verbatim.
"NB: You have no assignment for the case of ==50 or ==0.5"
Actually, the else should cover all scenarios not specifically called out by one of the conditional expressions.
dpb
dpb 2019년 8월 27일
편집: dpb 2019년 8월 27일
Well, yes, but I doubt if that's what OP intended it to do given the other three conditions. I should, I suppose, have made that "explicit assignment"
The answer from Andrei solves it!
... but still a bit irked why the original methodology isn't working. Code and variables attached...
It is working; you just left off the trailing ";" after a couple of the assignment lines in the code so it's doing as you ask and echoing the whole table out each iteration that one of those conditionals is executed.
Add the missing semicolons and all will be quiet...
I ran the code in your question using the data attached to your comment above. The loop is not getting stuck nor is it repeating the same column as you described. The loop runs as it should.
To confirm that, run this version below. The the only differences are
  • semicolons are used to suppress unnecessary output.
  • a counter "c" is used to count each iteration of the for-loop and the count is displayed so you can visually confirm that the loop has the expected 714 iterations.
load spxpmi2
data.signal = zeros(size(data.NAPMPMIIndex));
c = 0;
for indx=1:714
c = c+1;
disp(c)
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
ah, thanks very much.
I'm quite new to this but that is very much a rookie error
:-)

댓글을 달려면 로그인하십시오.

 채택된 답변

kk = [data.NAPMPMIIndex > 50, data.AboveMA > .5];
g = findgroups(kk(:,1),kk(:,2));
a = [-1;1;-.5;.5];
data.signal = a(g);

댓글 수: 3

Nicely done! Hadn't thought of the findgroups route.
That's great! Works perfectly!
Hi Nskel! Maybe we will accept this answer, huh? Excuse me.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Historical Contests에 대해 자세히 알아보기

제품

태그

질문:

2019년 8월 26일

댓글:

2019년 8월 30일

Community Treasure Hunt

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

Start Hunting!

Translated by