conditional statements on matrices

2 visualizaciones (últimos 30 días)
puccapearl
puccapearl el 8 de Abr. de 2024
Comentada: Voss el 8 de Abr. de 2024
Hi, I have two matrices like below:
I want to search when the 2nd column equals 0 on either matrix and make a new matrix that shows the 0 values along with its paired column 1 values. I also want to tag it to know from which matrix it came from.
Thank you! I am new to conditional statements.

Respuesta aceptada

Voss
Voss el 8 de Abr. de 2024
Where M1 and M2 are your matrices:
idx1 = M1(:,2) == 0;
idx2 = M2(:,2) == 0;
tags = repelem([1; 2],[nnz(idx1) nnz(idx2)]);
result = [[M1(idx1,:); M2(idx2,:)] tags]
  9 comentarios
puccapearl
puccapearl el 8 de Abr. de 2024
oh my gosh! *face palm*
Thank you! I'm sorry for the run around!
Voss
Voss el 8 de Abr. de 2024
No problem! You're welcome!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

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

Community Treasure Hunt

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

Start Hunting!

Translated by