Subtracting Two Matrices of different sizes

21 views (last 30 days)
AR
AR on 18 Dec 2015
Commented: AR on 20 Dec 2015
How do I do the following subtraction: let X = [302 64] where all values in X are either 0s or 1s
A= X - X';
A=[302 64] - [64 302];
How do I find subtract these?
  3 Comments
Walter Roberson
Walter Roberson on 18 Dec 2015
For any one entry in the result, say A(J,K), how should A(J,K) be computed?

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 18 Dec 2015
T1 = X; %the left side
T2 = X'; %whatever the right side is
if any(size(T1) < size(T2))
T1(size(T2,1),size(T2,2)) = 0; %zero pad
end
if any(size(T2) < size(T1))
T2(size(T1,1),size(T1,2)) = 0; %zero pad
end
A = T1 - T2;

More Answers (1)

Renato Agurto
Renato Agurto on 18 Dec 2015
Hi
do you mean something like this?
%A,B: inputs matrices
%C output matrix
N = max([size(A,1) size(B,1)]);
M = max([size(A,2) size(B,2)]);
An = zeros(N,M);
Bn = zeros(N,M);
An(1:size(A,1), 1:1:size(A,2)) = A;
Bn(1:size(B,1), 1:1:size(B,2)) = B;
C = An-Bn
  1 Comment
AR
AR on 20 Dec 2015
Renato, Thanks. This way worked too. Allen

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!