a easier way to do vector

조회 수: 3 (최근 30일)
Johnny
Johnny 2019년 12월 8일
댓글: Stephen23 2019년 12월 8일
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)

채택된 답변

Stephen23
Stephen23 2019년 12월 8일
편집: Stephen23 2019년 12월 8일
"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)
  댓글 수: 2
Johnny
Johnny 2019년 12월 8일
Thank you very much for your answer.
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
Stephen23
Stephen23 2019년 12월 8일
@Kenny Yu: see my editted answer. Note that MATLAB plots the columns of a matrix.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by