How to use if statement for than one variables?
7 views (last 30 days)
Show older comments
Ivan Mich
on 4 Jul 2021
Edited: Sulaymon Eshkabilov
on 4 Jul 2021
I would like to set one if statement. I am using the following code:
filename2= 'OutputFile1L.xlsx';
[d2,tex]= xlsread(filename2);
a=d2(:,1);
for ii=1:numel(a)
if a(ii)==0
b(ii)==1 & c(ii)==0
elseif a(ii)==1
b(ii)==0 & c(ii)==0
elseif a==2
b(ii)==0 & c(ii)==0
else
b(ii)==nan & c(ii)==nan
end
but command window shows me
Undefined function or variable b
What is the wrong? could you please help me?
0 Comments
Accepted Answer
Sulaymon Eshkabilov
on 4 Jul 2021
Edited: Sulaymon Eshkabilov
on 4 Jul 2021
Note that for loop and "if" based calcs are very slow and inefficient.
Anyhow if indeed you wish to fix your code, then check these corrected errs with ==, & |, , e.g:
for ii=1:numel(a)
if a(ii)==0
b(ii)=1; c(ii)=0;
elseif a(ii)==1 | a(ii)==2
b(ii)=0; c(ii)=0;
else
b(ii)=nan; c(ii)=nan;
end
end
2 Comments
Image Analyst
on 4 Jul 2021
Edited: Image Analyst
on 4 Jul 2021
For loops are not always slower than vectorized code - that's a myth from long ago. I ran your code both ways:
d2 = rand(1000, 2);
a=d2(:,1);
% Vectorized method:
tic
Idx=find(a(:)==0 | a(:)==1 | a(:)==2);
Idx2=find(a~=0 & a~=1 & a~=2);
b(Idx)=0;
b(Idx2)=nan;
c=b;
elapsedTime1 = toc
% For loop method.
tic
for ii=1:numel(a)
if a(ii)==0 | a(ii)==1 | a(ii)==2
b(ii)=0;
else
b(ii)=nan;
end
end
c=b;
elapsedTime2 = toc
I find:
elapsedTime1 =
4.8e-05
elapsedTime2 =
1.5e-05
so your for loop code takes less than a third of the time of your vectorized code.
Of course the two codes don't do exactly the same thing. If I make them more efficient and do the comparison 1000 times, with this code:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
% Initialize variables:
a = rand(1000, 1);
b = nan(size(a));
numTrials = 1000;
vecWon = 0;
for t = 1 : numTrials
% Find where a is 0, 1, or 2 and set it = 0 there.
% Try it vectorized first.
vectorizedStartTime = tic;
linearIndexes = find(a(:)==0 | a(:)==1 | a(:)==2);
b(linearIndexes)=0;
elapsedTimeVectorized = toc(vectorizedStartTime);
% Now try it with a for loop
b = nan(size(a)); % Reset
forStartTime = tic;
for k = 1 : numel(a)
if a(k)==0 || a(k)==1 || a(k)==2
b(k)=0;
else
b(k)=nan;
end
end
elapsedTimeFor = toc(forStartTime);
fprintf('At iteration %d\n Vectorized took %.7f seconds.\n For loop took %.7f seconds.\n', ...
k, elapsedTimeVectorized, elapsedTimeFor);
% See who was faster.
if elapsedTimeVectorized < elapsedTimeFor
vecWon = vecWon + 1;
end
end
vecPercentage = 100 * vecWon / numTrials;
forPercentage = 100 * (numTrials - vecWon) / numTrials;
fprintf('--------------------------------------------------\n');
fprintf('Vectorized won %3d times out of %d = %.1f%%.\nFor loop won %3d times out of %d = %.1f%%.\n', ...
vecWon, numTrials, vecPercentage, (numTrials - vecWon), numTrials, forPercentage);
We see
--------------------------------------------------
Vectorized won 182 times out of 1000 = 18.2%.
For loop won 818 times out of 1000 = 81.8%.
Now if we improve it even further for the vectorized code by using logical indexes rather than linear indexes:
logicalIndexes = a==0 | a==1 | a==2;
b(logicalIndexes) = 0;
we see that vectorized wins the majority of the time, but not always:
--------------------------------------------------
Vectorized won 903 times out of 1000 = 90.3%.
For loop won 97 times out of 1000 = 9.7%.
Sulaymon Eshkabilov
on 4 Jul 2021
@Image Analyst Very Good point and discussion! "Not Always" cases are small size simulation problems which require insignifcantly small amount of sim time.
More Answers (2)
Yongjian Feng
on 4 Jul 2021
You meant this?
if a(ii)==0
b(ii)=1;
c(ii)=0;
elseif a(ii)==1
b(ii)=0;
c(ii)=0;
elseif a==2
b(ii)=0;
c(ii)=0;
else
b(ii)=nan;
c(ii)=nan;
end
1 Comment
Sulaymon Eshkabilov
on 4 Jul 2021
Edited: Sulaymon Eshkabilov
on 4 Jul 2021
There are a couple of ERRs in Feng's proposed code:
for ... % is missing
...
if ..
elseif a==2 % MUST be a(ii)==2
...
end
end
Sulaymon Eshkabilov
on 4 Jul 2021
Edited: Sulaymon Eshkabilov
on 4 Jul 2021
There are several errs in your loop code that is not advised. Just because it is slow and inefficient.
Here is an easy sol isntead of loop based code:
...
a=d2(:,1);
Idx0=find(a(:)==0);
Idx1=find(a(:)==1 | a(:)==2);
Idx2=find(a~=0 & a~=1 & a~=2);
b(Idx0)=1; c(Idx0)=0;
b(Idx1)=0; c(Idx1)=1;
b(Idx2)=nan; c(Idx2)=nan;
0 Comments
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!