Forming loop to simplify codes

1 回表示 (過去 30 日間)
Johnny
Johnny 2019 年 12 月 10 日
回答済み: per isakson 2019 年 12 月 11 日
Can you help me use loop to simplify the following code and produce the same graph?
c=5
dista=log([12:-1:8 6 4:-1:1]+retint)
distb=log([14:-1:9 6 3:-1:1]+retint)
distc=log([16:-1:10 6 2:-1:1]+retint)
etaa = exp(-c*abs(dista-dista(:)));
discrima = 1./sum(etaa,1)
etab = exp(-c*abs(distb-distb(:)));
discrimb = 1./sum(etab,1)
etac = exp(-c*abs(distc-distc(:)));
discrimc = 1./sum(etac,1)
figure (11)
hold on
plot(discrima,'-mo','Displayname','gap = 12(8,6,4)1')
plot(discrimb,'-ro','Displayname','gap = 14(9,6,3)1')
plot(discrimc,'-bo','Displayname','gap = 16(10,6,2)1')
axis([0 length(dista) 0 1])
xlabel('Serial Position','fontsize',14)
ylabel('Discriminability','fontsize',14)
title({'SIMPLE Memory Model'...
'\fontsize{9}\color{blue}Length = 10; Various Gaps'})
lgd = legend ('Location','northwest');
lgd.NumColumns = 1;
lgd.FontSize = 9;
set(gcf, 'color', 'white');
hold off

採用された回答

per isakson
per isakson 2019 年 12 月 11 日
"produce the same graph" The code below produces the same graph.
However
  • it may be disputed whether it's simpler
  • less intermediate results are saved
%%
c = 5;
retint = 0;
%%
cac = { '-go','Displayname','gap = 12(8,6,4)1'
'-ro','Displayname','gap = 14(9,6,3)1'
'-bo','Displayname','gap = 16(10,6,2)1' };
num = [ [12:-1:8,6,4:-1:1]+retint
[14:-1:9 6 3:-1:1]+retint
[16:-1:10 6 2:-1:1]+retint ];
%%
figure(11);
hold on
%%
for jj = 1 : 3
dist = log(num(jj,:));
eta = exp(-c*abs(dist-reshape(dist,[],1)));
discrim = 1./sum(eta,1);
plot( discrim, cac{jj,:} )
end
%%
axis([0,length(dist(1,:)),0,1.02*max(max(discrim))])
xlabel('Serial Position','fontsize',14)
ylabel('Discriminability','fontsize',14)
title({'SIMPLE Memory Model'...
'\fontsize{9}\color{blue}Length = 10; Various Gaps'})
lgd = legend ('Location','northwest');
lgd.NumColumns = 1;
lgd.FontSize = 9;
set(gcf, 'color', 'white');
hold off

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by