How to compare two matrix?

1 次查看(过去 30 天)
ASHA PON
ASHA PON 2023-1-2
评论: Voss 2023-1-2
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]

采纳的回答

Voss
Voss 2023-1-2
编辑:Voss 2023-1-2
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 个评论
ASHA PON
ASHA PON 2023-1-2
Thank you for the answer. This is what i needed.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by