Adding uneven matrices

조회 수: 14 (최근 30일)
B T
B T 2012년 3월 6일
댓글: Walter Roberson 2018년 2월 14일
Hey guys,
is there a method(aka function) by which I can add two matrices that aren't the same size and make it so that the matrices become of equivalent size by being filled with zeros in the remaining space?
Thanks
[Merged information from Answer]
It would be like ..
a=ones(150,91)*2
b=ones(141,100)*3
a+b
So, is there a way to make the matrices even but filling them with zeros? (adding 0 more rows to b, and 9 more columns to a of value=0) ...
Thanks!
  댓글 수: 2
Andrei Bobrov
Andrei Bobrov 2012년 3월 6일
please get the your example
Walter Roberson
Walter Roberson 2012년 3월 6일
There are over 100,000 different possible results for that calculation. You need to be very specific about which elements are to be added to which elements.

댓글을 달려면 로그인하십시오.

답변 (2개)

Jan
Jan 2012년 3월 6일
a = ones(150,91)*2
b = ones(141,100)*3
c = AddFill(a, b);
function c = AddFill(a, b);
sa = size(a);
sb = size(b);
c = zeros(max(sa, sb)); % Pre-allocate
c(1:sa(1), 1:sa(2)) = a; % Assign a
c(1:sb(1), 1:sb(2)) = c(1:sb(1), 1:sb(2)) + b; % Add b
  댓글 수: 2
Ismail Qeshta
Ismail Qeshta 2018년 2월 14일
Hi Jan. Can you please let me know how to modify your code for three unequal matrices instead of two? Thank you.
Walter Roberson
Walter Roberson 2018년 2월 14일
function d = AddFill(a, b, c);
sa = size(a);
sb = size(b);
sc = size(c);
d = zeros(max([sa; sb; sc])); % Pre-allocate
d(1:sa(1), 1:sa(2)) = a; % Assign a
d(1:sb(1), 1:sb(2)) = d(1:sb(1), 1:sb(2)) + b; % Add b
d(1:sc(1), 1:sc(2)) = d(1:sc(1), 1:sc(2)) + c; % Add c

댓글을 달려면 로그인하십시오.


Andrei Bobrov
Andrei Bobrov 2012년 3월 6일
variant (one of 100000) :)
function out = addiffmatrix(varargin)
n = cellfun('ndims',varargin);
N = max(n);
sz = cell2mat(cellfun(@(x)[size(x) ones(1,N - numel(size(x)))],varargin(:),'un',0));
mm = arrayfun(@(i1)1:i1,sz,'un',0);
m1 = zeros(max(sz));
out = m1;
M = m1;
for j1 = 1:numel(varargin)
M(mm{j1,:}) = varargin{j1};
out = out + M;
M = m1;
end
using
a = ones(3,4)
b = 2*ones(5,3)
out = addiffmatrix(a,b)
out =
3 3 3 1
3 3 3 1
3 3 3 1
2 2 2 0
2 2 2 0
ADD
variant with "general point"
function out = addiffmatrix(cp,varargin)
% cp - coordinates of the general point in every matrix
% varargin - matrices
n = cellfun('ndims',varargin);
N = max(n);
sz = cell2mat(cellfun(@(x)[size(x) ones(1,N - numel(size(x)))],...
varargin(:),'un',0));
C = max(cp);
ij1 = bsxfun(@minus,C,cp);
mm = arrayfun(@(i1,j1)(1:i1)+j1,sz,ij1,'un',0);
out = zeros(C+max(sz-cp));
for j1 = 1:numel(varargin)
out(mm{j1,:}) = out(mm{j1,:}) + varargin{j1};
end
using
>> a
a =
1 1 1 1
1 1 1 1
>> b
b =
1.5 1.5 1.5
1.5 1.5 1.5
1.5 1.5 1.5
>> c
c =
2 2
2 2
2 2
2 2
2 2
>> cp
cp =
1 1
1 3
5 2
>> out = addiffmatrix(cp,a,b,c)
out =
0 2 2 0 0 0
0 2 2 0 0 0
0 2 2 0 0 0
0 2 2 0 0 0
1.5 3.5 4.5 1 1 1
1.5 1.5 2.5 1 1 1
1.5 1.5 1.5 0 0 0
>>

카테고리

Help CenterFile Exchange에서 Triangulation Representation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by