if statement - Matlab simple matrix manip -
i'm new matlab , want achieve simple operation : have 792 x 1046 uint8 matrix called mg , want convert cells values (from 0 255) values between 1 , 4 (1,2,3,4) in new matrix called mgc accordingly simple conditions.
strangely, new matrix filled 1s , 2s not 3s or 4s...
here code :
[x,y]=size(mg); mgc = zeros(x,y); i=1:x j=1:y if (mg(i,j)<=100) mgc(i,j)=1; elseif (100<mg(i,j)<=110) mgc(i,j)=2; elseif (110<mg(i,j)<=120) mgc(i,j)=3; else mgc(i,j)=4; end end end if me solve stupid issue, great ! thx
you shouldn't use expressions such 100<mg(i,j)<=110 in matlab. instead, use 100<mg(i,j) && mg(i,j)<=110.
at moment, matlab evaluating expression 100<mg(i,j)<=110 (100<mg(i,j))<=110. (100<mg(i,j)) going either 1 or 0 (true or false), , therefore <=110. never gets past second else, , array either 1 or 2.
edit: although answer explains specific issue you're having, should instead using logical indexing, more efficient double loop (and more idiomatic in matlab). see answers @excaza or @benoit_11 examples of that).
Comments
Post a Comment