Matrix logical matlab

I am trying to get M. To do this, it is necessary for Matlab to relate column 1 of A to column 1 of 𝐡^𝑇and build a matrix M with 1 and 0 depending on whether in position 𝐴[𝑖,𝑗] and 𝐡^𝑇[𝑖,𝑗] are equal to 1

A = [1 0 1; 0 1 1; 0 0 1 ]; 
B = [0 0 1 ; 0 1 0; 1 1 1];


for i = 1:3
  for j =1:3
    if A(i,j) == BT(i,j)  && A(i,j)==1;
       Z(i,j) = 1
   end
  end
end

Upvotes: 0

Views: 57

Answers (1)

magnesium
magnesium

Reputation: 485

When you use "if A(i,j) == BT(i,j) && A(i,j)==;" you are comparing individual elements. Instead you want to be comparing columns: A(:, i) and BT(:, j).

Precisely, you want

for i = 1:3
  for j = 1:3
       M(i,j) = any(  A(:,i) & BT(:,j)  );
  end
end

OR

You are comparing the columns of BT and the columns of A.

That is to say, the rows of B and the columns of A. You want to see if there are any occasions when both of the elements are 1. Thus you can compare the products of the terms in the rows of B and columns of A.

i.e. M = logical(B * A) should also give you the desired output.

NOTE that the data in B are different in your image examples and in your code.

Upvotes: 1

Related Questions