Just learning matlab question

1 view (last 30 days)
Robert
Robert on 2 Feb 2012
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

Geoff
Geoff on 3 Feb 2012
It's hard to answer this without giving you the answer... =) But here goes an attempt...
You can do boolean operations on a vector or matrix just as you would on a scalar value. Just compare the matrix to a value and you will get a matrix of booleans back.
That's the best I can offer without saying exactly what you might do. If you want clarification, feel free to ask =)
  3 Comments
Geoff
Geoff on 30 Mar 2012
Sweet as. Trial and error is the best way to learn, with a little guidance when you get stuck. =) I'm a self-taught programmer via exactly that approach. There was no internet to ask questions, so I just experimented until I worked it out, and read code in books to try and understand how computers work. It's amazing what you learn by dabbling that you don't learn by being shown! Needless to say, it's frustrating to see some people here, faced with one of the friendliest and most harmless programming languages around, freaking out and being afraid to just try things out (or pay attention to what the error messages are saying). Glad that you are trying harder than that. =) Keep it up.
Walter Roberson
Walter Roberson on 30 Mar 2012
You had books? Why, when I was learning programming, we...
Urrr, actually we _did_ have books, but the books back then assumed you had an intense interest in the bus cycle diagrams, the instruction-by-instruction timing and how it varied for each addressing mode, and of the inertial properties for the paper tape reader. User manuals were a few years off yet.
If I recall correctly, in those days, the largest community college had 5 computers, total, including the finance system (my father gave me a tour of them; I gather he had repaired 3 of them.)

Sign in to comment.

More Answers (1)

Walter Roberson
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

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!