How to compare two matrix?

6 views (last 30 days)
ASHA PON
ASHA PON on 2 Jan 2023
Commented: Voss on 2 Jan 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]

Accepted Answer

Voss
Voss on 2 Jan 2023
Edited: Voss on 2 Jan 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 Comments
ASHA PON
ASHA PON on 2 Jan 2023
Thank you for the answer. This is what i needed.
Voss
Voss on 2 Jan 2023
You're welcome!

Sign in to comment.

More Answers (0)

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!