How to use If elseif else in array ?
20 views (last 30 days)
I have variables of the image call 'IGray' in picture below
now I want to write the if elseif code to find amount of the value that I separate in 3 ways
- the value < 50
- the value 50-220
- the value > 220
primary, I try to write but it can not . As shown below
which I want ThW, ThWB and ThB show in the workspaces and it can double click to show all variables.
Please help me TT
Thank you very much ^^
YT on 6 Feb 2019
So everytime I have to do somethings similar, I either do it by using a for-loop and if/else statement and comparing every field of the matrix (ii,jj) to the condition OR I use logical indexing. So I've provided both options to show you the difference in the amount of code and the execution time.
% dummy matrix
A = randi([0 255],1171,1591);
ThB1 = 0;
ThBW1 = 0;
ThW1 = 0;
% with for-loop and if/else (I like this...)
for ii = 1:size(A,1)
for jj = 1:size(A,2)
if(A(ii,jj) <= 50)
ThB1 = ThB1 + 1;
elseif(((A(ii,jj) > 50) && (A(ii,jj) <= 220)))
ThBW1 = ThBW1 + 1;
ThW1 = ThW1 + 1;
% Elapsed time is 0.055028 seconds.
% alternative logical indexing (...but this is brilliant)
ThB2 = sum((A <= 50),'all');
ThBW2 = sum(((A > 50) & (A <= 220)),'all');
ThW2 = sum((A > 220),'all');
% Elapsed time is 0.003860 seconds.