Extract columns into new variables efficently

5 visualizaciones (últimos 30 días)
Santos García Rosado
Santos García Rosado el 21 de Abr. de 2021
Comentada: Santos García Rosado el 22 de Abr. de 2021
Hi Mathwork's community,
So I have a Matrix A such as:
A= [1 1 2 2 3 3 1 1 2 2 3 3; 1 1 2 2 3 3 1 1 2 2 3 3];
And a matrix B, which is the matrix that I'd like to separate into three different variables. (Note: There will always be three variables).
B = [10 20 30 40 50 60 70 80 90 100 110 120; 1 2 3 4 5 6 7 8 9 10 11 12];
Variable1 corresponds to B positions where A values are equal to 1, Variable2 corresponds to B positions where A values are equal to 2 and Variable3 corresponds to B positions where A values are equal to 3. The expected output should be:
Variable1 =[10 20 70 80; 1 2 7 8]
Variable2 = [30 40 90 100 ; 3 4 9 10]
Variable3 = [50 60 110 120; 5 6 11 12]
Could somone please help me out?
Santos
  2 comentarios
Walter Roberson
Walter Roberson el 21 de Abr. de 2021
What do you want to have happen for the case where the first row of A has a different value than the corresponding position in the second row of A?
Will the three unique values in A always be 1, 2, and 3, or could it be (for example) 7, 29, 31 ? Negatives? Fractions?
Santos García Rosado
Santos García Rosado el 21 de Abr. de 2021
Hello Walter. Thank's for the question. Clearly, I didn't make my-self clear enoguh on that part. All the rows of A will be equal to the first row and will always be 1, 2 and 3 values.

Iniciar sesión para comentar.

Respuesta aceptada

Jeff Miller
Jeff Miller el 21 de Abr. de 2021
A= [1 1 2 2 3 3 1 1 2 2 3 3; 1 1 2 2 3 3 1 1 2 2 3 3];
B = [10 20 30 40 50 60 70 80 90 100 110 120; 1 2 3 4 5 6 7 8 9 10 11 12];
type1 = A(1,:) == 1;
Variable1 = B(:,type1);
% et cetera

Más respuestas (0)

Categorías

Más información sobre Matrices and Arrays en Help Center y File Exchange.

Productos


Versión

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by