finding non-zero entries of a matrix

Hi All,
I've 2 matrices, A and B.
A = [1 0 2; 3 4 5; 0 0 0];
B = [0 0 0; 1 0 0; 1 1 1];
I want to generate a matrix C which assigns 1 to non-zero entries in both A and B.
C = [1 0 1; 1 1 1; 1 1 1]
For just A , I could do
C = zeros(3)
C(find(A~=0)) = 1
I'd like to ask for suggestion on how to assign 1 for all non-zero entries in both A and B.

 Accepted Answer

C=A|B;

2 Comments

Matt J
Matt J on 18 Feb 2021
Edited: Matt J on 18 Feb 2021
Or C=double(A|B) if you want the result in floating point.
+1
Had just come back to add, Matt... :)

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2019b

Tags

Asked:

on 18 Feb 2021

Commented:

dpb
on 18 Feb 2021

Community Treasure Hunt

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

Start Hunting!