Filter löschen
Filter löschen

Need help with If statement

1 Ansicht (letzte 30 Tage)
Aftab Ahmed Khan
Aftab Ahmed Khan am 31 Dez. 2014
Bearbeitet: Jan am 31 Dez. 2014
Hello everyone, My question is, in the below code, idsabs can take any value from 1 to 16. Depending on the value of idsabs, i want to set the value of idcabs. Is there any other way of doing this, because i think its too long just to get a single value. Thank you so much and Happy New Year.
if idsabs>=1 && idsabs<=2
idcabs=1;
elseif idsabs>=3 && idsabs<=4
idcabs=3;
elseif idsabs>=5 && idsabs<=6
idcabs=4;
elseif idsabs>=7 && idsabs<=8
idcabs=6;
elseif idsabs>=9 && idsabs<=10
idcabs=5;
elseif idsabs>=11 && idsabs<=12
idcabs=7;
elseif idsabs>=13 && idsabs<=14
idcabs=8;
elseif idsabs>=15 && idsabs<=16
idcabs=2;
end
  3 Kommentare
Aftab Ahmed Khan
Aftab Ahmed Khan am 31 Dez. 2014
Bearbeitet: Aftab Ahmed Khan am 31 Dez. 2014
Hi Stephen, idsabs will take only whole numbers between 1 to 16. No decimal values.
Jan
Jan am 31 Dez. 2014
If you have checked for idsabs<=2, there is no reason to check "idsabs>=3" anymore.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Stephen23
Stephen23 am 31 Dez. 2014
Bearbeitet: Stephen23 am 31 Dez. 2014
If adsabs only has whole values, then you could simply use a look-up table:
vec = [1,3,4,6,5,7,8,2];
adcabs = vec(ceil(adsabs/2));
This produces exactly the same values for adcabs as your questions gives:
>> arrayfun(@(x) vec(ceil(x/2)), 1:16)
ans = [1 1 3 3 4 4 6 6 5 5 7 7 8 8 2 2]
If you can tell us something about how the order of adcabs was generated, then maybe we could come up with a small function instead.
  1 Kommentar
Aftab Ahmed Khan
Aftab Ahmed Khan am 31 Dez. 2014
Thank you for your help. Its done.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Jan
Jan am 31 Dez. 2014
Bearbeitet: Jan am 31 Dez. 2014
A little bit simpler without "compression" of the look-up-table:
LUT = [1, 1, 3, 3, 4, 4, 6, 6, 5, 5, 7, 7, 8, 8, 2, 2];
idcabs = LUT(idsabs);

Kategorien

Mehr zu Multidimensional Arrays finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by