Sunday, 7 September 2014

to print natural numbers repeating each number fixed number of times

To generate output as shown below:
1
1
1
2
2
2
3
3
3

the code is
myfun=[];
for i = 1:3
temp=ones(3,1)*i; %the first parameter in ones function i,e, 3 can be changed to any number, say 6 to print six 1's and then six 2's and so on......
myfun= [myfun;temp];
end

No comments:

Post a Comment