How to compare two matrix?

6 visualizaciones (últimos 30 días)
ASHA PON
ASHA PON el 2 de En. de 2023
Comentada: Voss el 2 de En. de 2023
I am having two matrix of sizes 7*7 and 7*1. Now, i need to compare both matrix and entries in one matrix varies based on another matrix. For example, element in fourth row first column of matrix A is 1 and element in first row of matrix B is, so, expected output at matrix C is element 1 in fourth row of first column, whereas remaining terms are 0. Similarly, element in first row of fourth column is 1 in matrix A and fourth entry of matrix B is 0, so output matrix C has 0 element in first row of fourth column. Thank you in advance.
Example:
A=[0 0 0 1 0 0 0
0 1 0 0 0 0 0
0 0 0 0 1 0 0
1 0 0 0 0 0 0
0 0 0 0 0 1 0
0 0 1 0 0 0 0
0 0 0 0 0 0 1]
B=[1; 1; 0; 0; 1; 0; 1]
Expected output:
C=[0 0 0 0 0 0 0
0 1 0 0 0 0 0
0 0 0 0 1 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 1]

Respuesta aceptada

Voss
Voss el 2 de En. de 2023
Editada: Voss el 2 de En. de 2023
One way:
A=[0 0 0 1 0 0 0
0 1 0 0 0 0 0
0 0 0 0 1 0 0
1 0 0 0 0 0 0
0 0 0 0 0 1 0
0 0 1 0 0 0 0
0 0 0 0 0 0 1];
B=[1; 1; 0; 0; 1; 0; 1];
C = zeros(size(A));
idx = logical(B);
C(:,idx) = A(:,idx)
C = 7×7
0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 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 1
  2 comentarios
ASHA PON
ASHA PON el 2 de En. de 2023
Thank you for the answer. This is what i needed.
Voss
Voss el 2 de En. de 2023
You're welcome!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

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

Etiquetas

Productos


Versión

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by