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

14 views (last 30 days)
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];

Accepted Answer

Cris LaPierre
Cris LaPierre on 28 May 2020
B=ones(length(A),30);
B=cumsum([A B],2)
C=B==21
  1 Comment
Cris LaPierre
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)

Sign in to comment.

More Answers (1)

Fangjun Jiang
Fangjun Jiang on 28 May 2020
B=A+(1:31);
index=B==21;
B(index)=1;
B(~index)=0

Categories

Find more on Get Started with MATLAB in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!