Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

Running if loop until a specific outcome

조회 수: 2 (최근 30일)
numnum
numnum 2017년 11월 20일
마감: MATLAB Answer Bot 2021년 8월 20일
I have two double vectors, a and b. Both have values for each time point t. I want to run a loop where
  • -for each time t,
  • --if a >= 0.5 and the next value that is >=0.5 is in a, outa = outa + 1
  • --if a >= 0.5 and the next value that is >=0.5 is in b, outc = outc + 1
  • --if b >= 0.5 and the next value that is >=0.5 is in b, outb = outb + 1
  • --if b >= 0.5 and the next value that is >=0.5 is in b, outc = outc + 1
How might I be able to do this?
  댓글 수: 3
numnum
numnum 2017년 11월 20일
they are double vectors.
whos a/b yields
Name: v1
Size: 1x2000000
Bytes: 16000000
Class:double
numnum
numnum 2017년 11월 20일
when i try this:
[i,j,v] = find(a>=0.5);
[k,l,m] = find(b>=0.5);
for ix=(2:max(length(i),length(k)))
outa=0;
outb=0;
outc=0;
if i(ix) < k(ix) && i(ix+1) < k(ix)
outa=outa+1;
end
if i(ix) < k(ix) && i(ix+1) > k(ix)
outc=outc+1;
end
if i(ix) > k(ix) && i(ix+1) < k(ix)
outc=outc+1;
end
if i(ix) > k(ix) && i(ix+1) > k(ix)
outb=outb+1;
end
end
I get the error Index exceeds matrix dimensions.
Error (line 129) if i(ix) < k(ix) && i(ix+1) < k(ix)

답변 (1개)

Image Analyst
Image Analyst 2017년 11월 20일
Evidently i and k are different lengths, so that when ix goes from 2 to max(length(i),length(k)), it will be too long for the shorter of those two vectors. Before the loop put this:
length(i)
length(k)
max(length(i),length(k))
Then tell me what is is when it throws the error (look in the workspace panel).
  댓글 수: 3
Image Analyst
Image Analyst 2017년 11월 20일
OK, so at some point, since ix runs from 2 to 219533, ix will take on the value of 48681 (one longer than i is). So when it does this
if i(ix) < k(ix) && i(ix+1) < k(ix)
which means
if i(48680) < k(48680) && i(48680+1) < k(48680)
Now i(48680+1) is i(48681). And since there is no 48681st element of (the badly named) i, it will thrown an error.
numnum
numnum 2017년 11월 20일
편집: numnum 2017년 11월 20일
Yeah they are pretty badly named.
Is there a way I can fix this, while still running through all of k's 219533 values?
there also seems to be another problem because my code outputs outa=0 outb=1 outc=0 , which is very far off from the values i should get

이 질문은 마감되었습니다.

Community Treasure Hunt

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

Start Hunting!

Translated by