Input content of vector into 2nd dimension of 2-D array

11 views (last 30 days)
I want to import the content of a 1D array (or vector) into new 2-D array, where the first dimension is zeros, and the second dimension is the content of my vector.
The vector (A) contains data points with size(A) = 1, 3330
Dimensions desired for the 2D array are: B (3350, 3330), where the first dimension i is filled with zeros, and the second dimension j is filled with the content of A.
I have tried the following:
for i=1:size(B, 1)
for j=1:size(B, 2)
B(i,:) = A;
end
end
but this fills both dimensions with A, as opposed to just filling the second dimension (j) with A.
Any help would be appreciated!

Answers (1)

Priysha Aggarwal
Priysha Aggarwal on 6 Jun 2019
Example : Let 'A' of dimension (1,5) is the input 1D array.
A = [1 2 3 4 5]
If you want your output 2D matrix 'B' to be of dimension (3,5) as :
B = [0 0 0 0 0
0 0 0 0 0
1 2 3 4 5]
then do the following :
B = zeros(3,5)
B(end,:) = A
What your code attached above does is :
%iterating over all rows of B
for i=1:size(B, 1)
%iterating over all columns of B
for j=1:size(B, 2)
% changing each row of B as A
B(i,:) = A;
end
end
Hence the output of this code on above example will be :
B = [1 2 3 4 5
1 2 3 4 5
1 2 3 4 5]
Hope this helps!

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!