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 vues (au cours des 30 derniers jours)
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];

Réponse acceptée

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

Connectez-vous pour commenter.

Plus de réponses (1)

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

Catégories

En savoir plus sur Get Started with MATLAB dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by