How to perform operations depending on a value in an array

16 ビュー (過去 30 日間)
Pilar Jiménez
Pilar Jiménez 2019 年 10 月 15 日
編集済み: per isakson 2019 年 10 月 16 日
Good day,
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];
data=2200;
for i=1:length(C)
if C(i)==5||6||7
N(i)=data/188;
elseif C(i)==8||9||10||11||12||13||14||15||16||17
N(i)=data/396;
else C(i)<=18
N(i)=data/1659.5;
end
end

採用された回答

per isakson
per isakson 2019 年 10 月 15 日
編集済み: per isakson 2019 年 10 月 16 日
This works accourding to your expectations - I think.
%%
C=[10,8,34,7,7,24,11,15,10,13,13,20,17,39,6,9,5,21];
data=2200;
for i=1:length(C)
if ismember( C(i), [5,6,7] )
N(i)=data/188;
elseif ismember( C(i), [8,9,10,11,12,13,14,15,16,17] )
N(i)=data/396;
elseif C(i)<=18
N(i)=data/1659.5;
else
keyboard
end
end
From the top of my head
  • 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
  • etc
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
>> 13&&17
ans =
logical
1
>> 13||17
ans =
logical
Thus all your conditions evaluate to true
>> 17==5||6||7
ans =
logical
1
>>
Or what do you think about
>> ['A':2:'H']
ans =
'ACEG'
>> ['A':' ':'z']
ans =
'Aa'
  2 件のコメント
Pilar Jiménez
Pilar Jiménez 2019 年 10 月 15 日
Thank you, it works correctly and is shorter than how I solved it. I prefer your answer.
It can also be solved in another way:
for i=1:length(C)
if C(i)==5||C(i)==6;
N(i)=data/188.5;
elseif C(i)==8||C(i)==9||C(i)==10||C(i)==11||C(i)==12||C(i)==13||C(i)==14||C(i)==15||C(i)==16||C(i)==17;
N(i)=data/396;
else C(i)>=18;
N(i)=data/1659.5;
end
end
per isakson
per isakson 2019 年 10 月 15 日
編集済み: per isakson 2019 年 10 月 16 日
I wouldn't be surpriced if your solution is faster.
else C(i)>=18; isn't documented either. I was about to write that it's an error, but Matlab doesn't think so. Now, I think this is a legacy from the time when every position on the punch-card was valuable. How do you think it is evaluated?
This
if ismember( C(i), [5:7] )
N(i)=data/188;
elseif ismember( C(i), [8:17] )
N(i)=data/396;
is more readable (and less typing). Or
elseif any( C(i)==[8:17] )
or
elseif C(i)>=8 && C(i)<=17

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeNumeric Types についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by