How to make simpler this at 'if condition'?
Show older comments
There is 'a' vector [6x9].
And I want to define 'ahat' depends on 'ii' value.
For example, if ii=3, 'ahat' use a(1,1),a(2,1),a(3,1),a(4,1),a(5,1),a(6,1)] this value.
But could I make simpler this?
if (ii<5)
ahat=[a(1,1),a(2,1),a(3,1),a(4,1),a(5,1),a(6,1)];
elseif ((4<ii)&&(ii<9))
ahat=[a(1,2),a(2,2),a(3,2),a(4,2),a(5,2),a(6,2)];
elseif ((8<ii)&&(ii<13))
ahat=[a(1,3),a(2,3),a(3,3),a(4,3),a(5,3),a(6,3)];
elseif ((12<ii)&&(ii<17))
ahat=[a(1,4),a(2,4),a(3,4),a(4,4),a(5,4),a(6,4)];
elseif ((16<ii)&&(ii<21))
ahat=[a(1,5),a(2,5),a(3,5),a(4,5),a(5,5),a(6,5)];
elseif ((20<ii)&&(ii<25))
ahat=[a(1,6),a(2,6),a(3,6),a(4,6),a(5,6),a(6,6)];
elseif ((24<ii)&&(ii<29))
ahat=[a(1,7),a(2,7),a(3,7),a(4,7),a(5,7),a(6,7)];
elseif ((28<ii)&&(ii<33))
ahat=[a(1,8),a(2,8),a(3,8),a(4,8),a(5,8),a(6,8)];
else ((32<ii)&&(ii<37))
ahat=[a(1,9),a(2,9),a(3,9),a(4,9),a(5,9),a(6,9)];
end
a = ahat;
Accepted Answer
More Answers (2)
Steven Lord
on 26 Dec 2018
Make some sample data.
a = reshape(randperm(6*9), [6 9])
ii = 15;
Define the cutoffs.
vec = [0 4 8 12 16 20 24 28 32 36];
% or
vec = 0:4:36;
Determine which bin your selected ii falls into. For this sample ii, it falls into bin #4 (between 12 and 16.)
d = discretize(ii, vec)
Create ahat.
ahat = a(1:6, d);
This will even work with a row vector ii.
ii = [15 21 28];
d = discretize(ii, vec)
ahat2 = a(1:6, d)
1 Comment
CHOI HYUNDUK
on 26 Dec 2018
madhan ravi
on 26 Dec 2018
Edited: madhan ravi
on 26 Dec 2018
a = rand(6,9) % some data to test
ii = 6 % according to our expectation the result should be the second column
if ii < 5
ahat = a(:,1) ;
elseif ( 4 < ii ) && ( ii < 9 )
ahat = a(:,2) ;
elseif ( 8 < ii ) && ( ii < 13 )
ahat = a(:,3) ;
elseif ( 12 < ii ) && ( ii < 17 )
ahat = a(:,4) ;
elseif ( 16 < ii ) && ( ii < 21 )
ahat = a(:,5) ;
elseif ( 20 < ii ) && ( ii < 25 )
ahat = a(:,6) ;
elseif ( 24 < ii ) && ( ii < 29 )
ahat = a(:,7) ;
elseif ( 28 < ii ) && ( ii < 33 )
ahat = a(:,8) ;
else ( 32 < ii ) && ( ii < 37 )
ahat = a(:,9) ;
end
a = ahat.'
Gives:
a =
Columns 1 through 7
0.1672 0.9516 0.5479 0.6663 0.9991 0.1904 0.6448
0.1062 0.9203 0.9427 0.5391 0.1711 0.3689 0.3763
0.3724 0.0527 0.4177 0.6981 0.0326 0.4607 0.1909
0.1981 0.7379 0.9831 0.6665 0.5612 0.9816 0.4283
0.4897 0.2691 0.3015 0.1781 0.8819 0.1564 0.4820
0.3395 0.4228 0.7011 0.1280 0.6692 0.8555 0.1206
Columns 8 through 9
0.5895 0.6171
0.2262 0.2653
0.3846 0.8244
0.5830 0.9827
0.2518 0.7302
0.2904 0.3439
ii =
6
a =
0.9516 0.9203 0.0527 0.7379 0.2691 0.4228
1 Comment
CHOI HYUNDUK
on 26 Dec 2018
Categories
Find more on Signal Processing 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!