Adding uneven matrices

13 visualizzazioni (ultimi 30 giorni)
B T
B T il 6 Mar 2012
Commentato: Walter Roberson il 14 Feb 2018
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 Commenti
Andrei Bobrov
Andrei Bobrov il 6 Mar 2012
please get the your example
Walter Roberson
Walter Roberson il 6 Mar 2012
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.

Accedi per commentare.

Risposte (2)

Jan
Jan il 6 Mar 2012
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 Commenti
Ismail Qeshta
Ismail Qeshta il 14 Feb 2018
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 il 14 Feb 2018
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

Accedi per commentare.


Andrei Bobrov
Andrei Bobrov il 6 Mar 2012
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
>>

Categorie

Scopri di più su Triangulation Representation in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by