Just learning matlab question
Show older comments
I just started taking a MATLAB class at school and we have a problem to do. I am a little stuck on something and I cannot find an answer in my notes or the book. Tha basic premise is this: I have one value call it x = 2 I have a set of distances, 1 2 3 4 5 and a formula which is M= x * distance. I just posted this in the newsgroups...I Am not sure wha tis more effective since I just started here.
We need to do the calculation which I did. here M would be 2 4 6 8 10 Then if M is greater than say 5 have matlab say one thing if less than 5 say another. I cant seem to get matlab to recognize my if statements. I dont want to be too specific because I dont want to just get the answer but the way to do it. Any help would be great. Thanks
Bob
Accepted Answer
More Answers (1)
Walter Roberson
on 3 Feb 2012
Use logical indexing. For example,
M5 = M > 5;
M(M5) = (M(M5) - 1) / 2; %the one case
M(~M5) = M(~M5) + 3; %the other case
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!