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

11 visualizzazioni (ultimi 30 giorni)
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];

Risposta accettata

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

Accedi per commentare.

Più risposte (1)

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

Categorie

Scopri di più su Get Started with MATLAB in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by