if statement within for within while loop is not fullfilled but loop is exited

조회 수: 1 (최근 30일)
we want to modify our randomization, to let matlab rerandomize the order (1-12) if some conditions are met.
So when the restrictions are met, we want the for loop to break, set z=0 to rerandomize within the while loop.
Only if none of the restrictions are violated, we want to set z=1 so that the randomization would not be executed again.
However this is not working with the following code, after running it a few times, some of the randomizations do not follow our conditions ... can anyone help? Thanks in advance
z=0;
while z==0
randStim = stimuli(randperm(size(stimuli,1)/2),:);
randomizeOrder(randomizeOrder(:,4)==1,7)=randStim;
for i=1:10
if randomizeOrder(randomizeOrder(:,7)==i,5) == randomizeOrder(randomizeOrder(:,7)==i+1,5) == randomizeOrder(randomizeOrder(:,7)==i+2,5) || ...
randomizeOrder(randomizeOrder(:,7)==i,2) == randomizeOrder(randomizeOrder(:,7)==i+1,2) == randomizeOrder(randomizeOrder(:,7)==i+2,2) || ...
randomizeOrder(randomizeOrder(:,7)==i,6) == randomizeOrder(randomizeOrder(:,7)==i+1,6) == randomizeOrder(randomizeOrder(:,7)==i+2,6)
z=0;
break
else
z=1;
end
end
end

채택된 답변

Walter Roberson
Walter Roberson 2020년 5월 12일
if randomizeOrder(randomizeOrder(:,7)==i,5) == randomizeOrder(randomizeOrder(:,7)==i+1,5) == randomizeOrder(randomizeOrder(:,7)==i+2,5) || ...
The first randomizeOrder(mask) == randomizeOrder(mask2) executes and returns a vector of logical results. You then compare that vector of logical results to randomizeOrder(mask3) which is unlikely to work.
If you want a comparison along the lines A == B == C meaning that A, B, and C all must be the same, then you need to expand it out, such as A==B & B==C

추가 답변 (1개)

dpb
dpb 2020년 5월 12일
if randomizeOrder(randomizeOrder(:,7)==i,5) == randomizeOrder(randomizeOrder(:,7)==i+1,5) == randomizeOrder(randomizeOrder(:,7)==i+2,5) || ...
randomizeOrder(randomizeOrder(:,7)==i,2) == randomizeOrder(randomizeOrder(:,7)==i+1,2) == randomizeOrder(randomizeOrder(:,7)==i+2,2) || ...
randomizeOrder(randomizeOrder(:,7)==i,6) == randomizeOrder(randomizeOrder(:,7)==i+1,6) == randomizeOrder(randomizeOrder(:,7)==i+2,6)
is nearly impossible to read for starters...but if is only true IFF all elements of a vector are nonzero -- altho if the elements of the array are unique (are they, I presume?) then that subscripting will return a single vector or empty set if not every element is in the set (but that would error so presume not the case).
OTOH, if they are NOT unique, then the result of the subscripting operation will return a vector and the comparison will never be true unless each and every element is the same.
I thought I had an idea when I started; realize I'm lost in following what you're after, specifically because don't understand the input well enough...so I'll leave as started but end up w/ the usual request--altho maybe the answer is as simple as needing parentheses to set precedence...
"Show us a sample dataset and explain what are after"
  댓글 수: 1
cazoe
cazoe 2020년 5월 12일
thank you for your thougths! The mistake was in what Walter Roberson described, the usage of two == in this logical statement... It works now

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

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by