Combining matrix of different dimension.

2 Ansichten (letzte 30 Tage)
Gratitude Kim
Gratitude Kim am 26 Jul. 2017
Beantwortet: Andrei Bobrov am 26 Jul. 2017
I have three matrix. Matrix 1 = [2 2; 0 0],Matrix 2 =[1 1;0 0] and Matrix 3=[2 2 2 2 ;3 3 3 3] . I will like to combine these 3 matrix such that it becomes [2 2 0 0;0 0 0 0;1 1 0 0;0 0 0 0;2 2 2 2;3 3 3 3]. Appreciate your kind assistance. Thanks.

Akzeptierte Antwort

Andrei Bobrov
Andrei Bobrov am 26 Jul. 2017
m1 = [2 2; 0 0];
m2 =[1 1;0 0];
m3=[2 2 2 2 ;3 3 3 3];
C = {m1,m2,m3};
[r,c] = cellfun(@size,C);
m = max([r(:),c(:)]);
for ii = 1:numel(C)
if any([r(ii),c(ii)] < m)
C{ii}(m(1),m(2)) = 0;
end
end
out = cat(1,C{:})

Weitere Antworten (1)

Moe_2015
Moe_2015 am 26 Jul. 2017
Bearbeitet: Moe_2015 am 26 Jul. 2017
One solution:
Matrix1 = [ 2 2; 0 0];
Matrix2 = [1 1;0 0];
Matrix3 = [2 2 2 2;3 3 3 3];
[reshape(Matrix1',1,4);zeros(1,4);reshape(Matrix2',1,4);zeros(1,4);Matrix3]
  2 Kommentare
Gratitude Kim
Gratitude Kim am 26 Jul. 2017
Thanks a lot.
Moe_2015
Moe_2015 am 26 Jul. 2017
No problem!

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by