# a easier way to do vector

2 views (last 30 days)
Johnny on 8 Dec 2019
Is there any easier or more efficient way to calculate SF when there is different k?
I = [1:100]
k03 = 0.3
k1 = 1
k3 = 3
k5 = 5
k8 = 8
k13 = 13
k15 = 15
k18 = 18
k23 = 23
k25 = 25
k28 = 28
S03F = k03*log(I)
S1F = k1*log(I)
S3F = k3*log(I)
S5F = k5*log(I)
S8F = k8*log(I)
S13F = k13*log(I)
S15F = k15*log(I)
S18F = k18*log(I)
S23F = k23*log(I)
S25F = k25*log(I)
S28F = k28*log(I)

Stephen Cobeldick on 8 Dec 2019
Edited: Stephen Cobeldick on 8 Dec 2019
"Is there any easier or more efficient way to calculate SF when there is different k?"
Do NOT use numbered variables, they are a sign that you are doing something wrong (in particular, you are forcing meta-data into variable names).
A much more efficient use of MATLAB would be to use vectors:
I = 1:100; % square brackets do nothing here
kV = [0.3,1,3,5,8,13,15,18,23,25,28]; % row vector
SV = bsxfun(@times,kV,log(I(:))) % MATLAB versions <R2016b
SV = kV.*log(I(:)) % MATLAB versions >=R2016b
plot(I,SV)

Johnny on 8 Dec 2019
Can I plot a graph with SV same as a graph with many SF? I use plot (SV) which looked very different as my previous graph.
this is my code for my original graph.
figure(1)
hold on
plot(S03F,'co-','markersize',3,'Displayname','k<1');
plot(S1F,'gs-','markersize',3,'Displayname','k=1');
plot(S3F,'gs-','markersize',3,'Displayname','k=3');
plot(S5F,'gs-','markersize',3,'Displayname','k=5');
plot(S8F,'gs-','markersize',3,'Displayname','k=8');
plot(S13F,'md-','markersize',3,'Displayname','k=13');
plot(S15F,'md-','markersize',3,'Displayname','k=15');
plot(S18F,'md-','markersize',3,'Displayname','k=15');
plot(S23F,'rp-','markersize',3,'Displayname','k=23');
plot(S25F,'rp-','markersize',3,'Displayname','k=25');
plot(S28F,'rp-','markersize',3,'Displayname','k=28');
lgd = legend ('Location','northwest');
lgd.NumColumns = 4;
lgd.FontSize = 6;
hold off
Stephen Cobeldick on 8 Dec 2019
@Kenny Yu: see my editted answer. Note that MATLAB plots the columns of a matrix.