MATLAB Answers

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

1 view (last 30 days)
cazoe
cazoe on 12 May 2020
Commented: cazoe on 12 May 2020
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

  0 Comments

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 12 May 2020
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

More Answers (1)

dpb
dpb on 12 May 2020
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 Comment

cazoe
cazoe on 12 May 2020
thank you for your thougths! The mistake was in what Walter Roberson described, the usage of two == in this logical statement... It works now

Sign in to comment.

Products


Release

R2018b

Translated by