# How do I write the below code in efficient way in MATLAB?

2 views (last 30 days)
Haitham AL Satai on 25 Sep 2022
Commented: Haitham AL Satai on 25 Sep 2022
I have below the following data
Q = [16,32,64,128,256,512,1024];
VEC_5 = [0.2380 0.2380 0.2380 0.2380 0.2380 0.2380 0.2380];
VEC_10 = [1.1898 1.1898 0.2380 0.2380 0.2380 0.2380 0.2380];
VEC_15 = [2.1416 2.1416 2.1416 1.1898 1.1898 0.2380 0.2380];
VEC_20 = [4.9970 3.0934 2.1416 2.1416 2.1416 1.1898 1.1898];
VEC_25 = [4.9970 4.9970 4.9970 3.0934 2.1416 2.1416 1.1898];
VEC_30 = [8.8043 5.9488 4.9970 4.9970 3.0934 2.1416 1.1898];
VEC_35 = [10.7079 8.8043 6.9007 4.9970 4.9970 2.1416 2.1416];
VEC_40 = [14.5152 10.7079 8.8043 6.9007 4.9970 3.0934 2.1416];
VEC_45 = [16.4188 11.6597 10.7079 8.8043 4.9970 4.9970 2.1416];
VEC_50 = [19.2742 14.5152 10.7079 8.8043 5.9488 4.9970 2.1416];
VEC_55 = [21.1779 16.4188 13.5634 10.7079 6.9007 4.9970 2.1416];
VEC_60 = [23.5574 16.4188 14.5152 10.7079 8.8043 4.9970 2.6175];
I am drawing them with semilogx function in Matlab as shown in the line of code below:
semilogx(Q,VEC_5, Q,VEC_10, Q,VEC_15, Q,VEC_20, Q,VEC_25, Q,VEC_30, Q,VEC_35, Q,VEC_40, Q,VEC_45, Q,VEC_50, Q,VEC_55, Q,VEC_60,'LineWidth',2,'MarkerEdgeColor','b')
I also added legend to them as shown below:
xticks(Q)
xlabel('Q order');
ylabel('Coverage');
legend('\theta_1_/_2 = 5','\theta_1_/_2 = 10','\theta_1_/_2 = 15','\theta_1_/_2 = 20','\theta_1_/_2 = 25','\theta_1_/_2 = 30','\theta_1_/_2 = 35','\theta_1_/_2 = 40','\theta_1_/_2 = 45','\theta_1_/_2 = 50','\theta_1_/_2 = 55','\theta_1_/_2 = 60')
grid on;
As you can see when I need to draw the data and put legends for them I have to take some time in order to insert them into the drawing function and legend. May I get some assistance by writing them in a simple piece of code by looping over them without writing them every time and adding a different color to every curve? I need all this data in one figure.

Torsten on 25 Sep 2022
Q = [16,32,64,128,256,512,1024];
VEC=[0.2380 0.2380 0.2380 0.2380 0.2380 0.2380 0.2380
1.1898 1.1898 0.2380 0.2380 0.2380 0.2380 0.2380
2.1416 2.1416 2.1416 1.1898 1.1898 0.2380 0.2380
4.9970 3.0934 2.1416 2.1416 2.1416 1.1898 1.1898
4.9970 4.9970 4.9970 3.0934 2.1416 2.1416 1.1898
8.8043 5.9488 4.9970 4.9970 3.0934 2.1416 1.1898
10.7079 8.8043 6.9007 4.9970 4.9970 2.1416 2.1416
14.5152 10.7079 8.8043 6.9007 4.9970 3.0934 2.1416
16.4188 11.6597 10.7079 8.8043 4.9970 4.9970 2.1416
19.2742 14.5152 10.7079 8.8043 5.9488 4.9970 2.1416
21.1779 16.4188 13.5634 10.7079 6.9007 4.9970 2.1416
23.5574 16.4188 14.5152 10.7079 8.8043 4.9970 2.6175];
cmap = jet(12);
hold on
for k = 1:12
semilogx(Q,VEC(k,:), 'Color', cmap(k, :),'LineWidth',2,'MarkerEdgeColor','b');
end
hold off
%semilogx(Q,VEC,'LineWidth',2,'MarkerEdgeColor','b')
xticks(Q)
xlabel('Q order');
ylabel('Coverage');
legend('\theta_1_/_2 = 5','\theta_1_/_2 = 10','\theta_1_/_2 = 15','\theta_1_/_2 = 20','\theta_1_/_2 = 25','\theta_1_/_2 = 30','\theta_1_/_2 = 35','\theta_1_/_2 = 40','\theta_1_/_2 = 45','\theta_1_/_2 = 50','\theta_1_/_2 = 55','\theta_1_/_2 = 60')
grid on;
Haitham AL Satai on 25 Sep 2022
@Torsten Thank you dear. Is it possible to add the legends?

### Categories

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

Translated by