1,2,2,3,3,3,4,4,4,4 & abbcccddddeeeee using MATLAB


1)Generate a vector like 1,2,2,3,3,3,4,4,4,4
So if n = 3, then return
[1 2 2 3 3 3]
And if n = 5, then return
 [1 2 2 3 3 3 4 4 4 4 5 5 5 5 5]

Code:

clc

x=input('Enter the number');
Y=[];
for i=1:x
    m=i;
    for i=1:m
        Y=[Y m];
    end
end
disp(Y)

Explanation:





Output:




2) Generate a string like abbcccddddeeeee
So if n = 3, then return
 'abbccc'
And if n = 5, then return
 'abbcccddddeeeee'

For this program , you should have a clear idea on STRING related programs in MATLAB.
If you already have idea well & good but if you don't know , please go through the video :

Code:


clc
x=input('Enter the number');
m=97;
Y=[];
Y=[Y m];
for i=1:x-1
    m=(97+i);
    for i=97:m
        Y=[Y m];
    end
end
a=char(Y);
disp(a)

ASCII TABLE:




Code can be improved like below:

clc
x=input('Enter the number');
if(x>=1 && x<=26)
m=97;
Y=[];
Y=[Y m];
for i=1:x-1
    m=(97+i);
    for i=97:m
        Y=[Y m];
    end
end
a=char(Y);
disp(a)
else
    disp('Enter input between 1 to 26');

end

Output:




No comments