Input content of vector into 2nd dimension of 2-D array
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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!
0 comentarios
Respuestas (1)
Priysha Aggarwal
el 6 de Jun. de 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!
0 comentarios
Ver también
Categorías
Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!