how to use certain values to write a for loop?
Show older comments
X = [67;89;78;56;55;75;99];
f = [4;5];
for i = 1:length(X);
if i == f;
X1(i,1) = 5*X(i,1);
else
X1(i,1) = X(i,1);
end
end
I know that the way i have written the if expression is wrong but can someone tell how do I write the if expression such that it uses the values of f.. basically if i equals the value in the f matrix or array the first formula is used else the second.
Thanks for help.
Sid
2 Comments
There is absolutely no point in using a slow and ugly loop. Here is Walter Roberson's much better solution in full:
>> X = [67;89;78;56;55;75;99];
>> f = [4;5];
>> X1 = X;
>> X1(f) = 5 * X1(f)
X1 =
67
89
78
280
275
75
99
Siddharth Parmar
on 25 Feb 2016
Accepted Answer
More Answers (1)
Walter Roberson
on 25 Feb 2016
Edited: Stephen23
on 25 Feb 2016
ismember(i, f)
Or, you can use
X1 = X;
X1(f) = 5 * X(f);
with no loop.
Categories
Find more on Loops and Conditional Statements 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!