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 visualizaciones (últimos 30 días)
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];

Respuesta aceptada

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

Iniciar sesión para comentar.

Más respuestas (1)

Fangjun Jiang
Fangjun Jiang el 28 de Mayo de 2020
B=A+(1:31);
index=B==21;
B(index)=1;
B(~index)=0

Categorías

Más información sobre Get Started with MATLAB en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by