- Matlab has been around since the seventies, that's more than fourty years.
- Back then there was one data type, matrix of doubles. A scalar was a <1x1> matrix.
- When doing conditional stuff zero was interpreted as false and non-zeros as true
- Text was ascii-numbers interpreted as characters
How to perform operations depending on a value in an array
15 views (last 30 days)
Hopefully you can help me with a question, I have a data array, with 18 specific data. I want to make certain operations based on the value in the array. What I don't know is how to write the conditions, I know I need an if and the operator "or" which is "||", I have used it but it doesn't do what I want, so I think I'm misusing the idea.
What I want to achieve is that if the value I have in array C, in the order in which it, is equal to 5, 6 or 7, the data variable is divided by 188, if the value is 8,9,10,11 , 12,13,14,15,16 or 17 the variable data is divided by 396 and if the value is greater than or equal to 18, then it is divided by 1659.5; and the result of the operation is stored in a new array called N for each value. But I have an error because the "or" operator is not used correctly and how to ensure that operations can be performed depending on the value.
I hope you can help me, thanks in advance.
This is the code I have:
C=[10 8 34 7 7 24 11 15 10 13 13 20 17 39 6 9 5 21];
per isakson on 15 Oct 2019
Edited: per isakson on 16 Oct 2019
This works accourding to your expectations - I think.
if ismember( C(i), [5,6,7] )
elseif ismember( C(i), [8,9,10,11,12,13,14,15,16,17] )
From the top of my head
This in combination with Mathworks' reluctance to break users code has resulted in Matlab containing old code that isn't documented.
|| and && is supposed to operate on logicals, however
Thus all your conditions evaluate to true
Or what do you think about
>> ['A':' ':'z']