Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Adding dummy zeroes in a matrix

Subject: Adding dummy zeroes in a matrix

From: Anand Anand

Date: 21 Mar, 2011 18:31:05

Message: 1 of 3

 I have so many arrays with varying lengths that need to be combined into a single matrix of order 'n' x n where n is the length of the longest array with dummy zeroes added in the to the arrays shorter than the longest array.For example,
I have an array A=1,2,3
and array B=1,2,3,4
I want to combine both A and B in a single matrix C such that C=[1 2 3 0
                                                                                          1 2 3 4]
A dummy zero has to be added after the last number in A so that the matrix C is a square..How do I achieve that?

Subject: Adding dummy zeroes in a matrix

From: Bruno Luong

Date: 21 Mar, 2011 19:04:04

Message: 2 of 3

Here is recent thread on the same question:

http://www.mathworks.com/matlabcentral/newsreader/view_thread/303572

Bruno

Subject: Adding dummy zeroes in a matrix

From: ImageAnalyst

Date: 21 Mar, 2011 19:05:29

Message: 3 of 3

Here's one way. I don't know how to do it any shorter because you may
have any number of crazily-named matrices that we can't refer to in a
systematic way (like building the name from a loop counter or
something).

A=[1,2,3]
lengthA = length(A)
B=[1,2,3,4]
lengthB = length(B)
myOtherMatrix = [1,2]
lengthMOM = length(myOtherMatrix)

maxLength = max([lengthA lengthB lengthMOM])
C = zeros(3, maxLength); % Initialize to all zeros.
C(1, 1:lengthA) = A;
C(2, 1:lengthB) = B;
C(3, 1:lengthMOM) = myOtherMatrix;

% Now display the final C
C

But I don't see why you say that C has to be square. What if the
longest vector is 100 elements long and you have only 7 matrices to
combine? The result would be 7 rows by 100 columns, which is not
square.

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us