How to repeat the first number only
1 次查看(过去 30 天)
显示 更早的评论
Array a is given
a =[1]
How can I add 0 to the array depending on n.
n=1 --> a=[0 1]
n=2 --> a=[0 0 1]
n=3 --> a=[0 0 0 1]
and n can be changes to any number
0 个评论
采纳的回答
madhan ravi
2018-12-7
编辑:madhan ravi
2018-12-7
a = 1;
n = 2; % just an example n
a = [zeros(1,n) a]
2 个评论
madhan ravi
2018-12-7
编辑:madhan ravi
2018-12-7
Anytime :) , respond to your previous question https://in.mathworks.com/matlabcentral/answers/434359-how-to-add-1-into-the-array#answer_350993
更多回答(1 个)
Image Analyst
2018-12-7
madhan's way is the best (how I'd do it), but I just want to show you a neat trick - that you can "grow" an array by simply assigning something to the last location (the location at the largest point that you want to grow it to):
a = 1; % Initial array.
n = 2; % just an example n
% a = [zeros(1,n) a] % Best way
a(n+1) = 0; % Enlarge array by appending zeros simply by assigning the last element.
a = fliplr(a) % Flipr it so that the 1 is on the right.
5 个评论
Stephen23
2018-12-7
编辑:Stephen23
2018-12-7
Not just a neat trick, until R2015b allocating to the last element (and implicitly filling array with zeros) was faster than calling zeros:
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!