How to add zeros in front of an array

19 views (last 30 days)
Hello ,
i want to add zeros in front of an array but i want to do that for 3 times and each time the zeros are increasing by1 .
eg 1st time : 0 array
2nd time : 00 array
3rd time : 000 array
I want to use a for-loop but i am getting errors like that :
for l=1:3
output(l,:)=[zeros(1,l) , example_array];
end
Any ideas?

Accepted Answer

Walter Roberson
Walter Roberson on 27 Dec 2019
output = cell(3,1);
for l=1:3
output{l}=[zeros(1,l) , example_array];
end
The reason what you tried failed is that the rows are all different lengths.
If you wanted to do
0 array 0 0
0 0 array 0
0 0 0 array
then you could do something close to what you had:
L = length(example_array);
output = zeros(3, L+3);
for l = 1 : 3
output(l, l+1:l+L) = example_array;
end

More Answers (0)

Categories

Find more on Multidimensional Arrays 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!