How to add matrix of different orders

조회 수: 6 (최근 30일)
simone clochiatti
simone clochiatti 2016년 4월 24일
답변: Peta 2016년 4월 24일
How can I add two matrix in which one is bigger than the other: A=[3 5 6 7;6 1 2 3] B=[2 4 5;5 7 8]
I want to make B the same order as A, adding zeros,in order to perform an addition between them, so B=[2 4 5 0;5 7 8 0] How do I perform this operation to B knowing the dimensions of A?

답변 (1개)

Peta
Peta 2016년 4월 24일
If this dynamically changes over time I would calculate the difference in the width of the matrices like this first:
sizediff = size(A,2)-size(B,2);
And then use the horzcat command to pad the B variable, either by writing this:
B = horzcat(B,zeros(size(B,1),sizediff))
or this:
B = [B,zeros(size(B,1),sizediff)]
or potentially even this:
B = cat(2,B,zeros(size(B,1),sizediff))

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by