Increasing value of each row from matrix A to create Matrix B, then replace 21 into 1 while the rest into 0 to create Matrix C

3 views (last 30 days)
JL on 28 May 2020
Commented: JL on 28 May 2020
I have matrix A,
A= [14
9
8
13
12];
I want to create matrix B where row value of matrix A increases 1 for subsequent columns (31 columns in total)
B= [15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42];
then after which create C matrix by converting non 21s to zero while 21s into 1
C= [0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0];

Cris LaPierre on 28 May 2020
B=ones(length(A),30);
B=cumsum([A B],2)
C=B==21
Cris LaPierre on 28 May 2020
I like Fangjun's way of creating B. It's unclear based on your values whether the first column of B should be A or A+1. It's easy enough to adjust:
B=A+(0:30)

Fangjun Jiang on 28 May 2020
B=A+(1:31);
index=B==21;
B(index)=1;
B(~index)=0
JL on 28 May 2020
Thanks Fangjun! But I have to pick Cris since he answered first. Is it possible to help with another question? https://www.mathworks.com/matlabcentral/answers/536635-auto-split-matrix-into-other-matrices-up-to-n