i started to learn matlab recently,and when i was studying about loops, one of my friend shared this code that can print a box with an X inside with the desired size
can someone provide explanation on this code?

댓글 수: 2

Cedric
Cedric 2017년 10월 15일
편집: Cedric 2017년 10월 15일
What part don't you understand specifically?
Please ask for specific details. It would be a waste of time to explain "clc", if you can read this by your own already:
help clc

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

 채택된 답변

KL
KL 2017년 10월 15일

0 개 추천

I suppose you read the documentation to understand what each command does and how a loop works. As far as this code is concerned, as you you can see, the idea is to plot a box with an 'x' inside it. It would have been easier to understand if the variables had much more meaningful names rather than 'x', 'n' and 'i'.
Anyway, here 'i' and 'x' are the row & column number of the box respectively and 'x' is decreasing from 'n' and 'i' is increasing from '1'. This is done so because you'd need to plot '*' along both diagonals to draw the letter 'x' inside the box.
So the while loop iterates through rows and the if condition in the beginning checks if it's the top or bottom most line of the square since in the case, '*' needed to plotted on every column, like a continuous line. then the for loop iterates over columns from 1 to n and plots a '*' if it's a diagonal position or a ' ' otherwise.
Got it?

추가 답변 (1개)

Priyanka Akre
Priyanka Akre 2020년 4월 22일
편집: Image Analyst 2022년 7월 21일

0 개 추천

% self organizing map Kohonen
% Programmed by Ammar AL-Jodah
clc
clear all
close all
for i=1:1000
x1(i)=rand;
x2(i)=rand;
end
for j1=1:10
for j2=1:10
w1(j1,j2)=rand*(0.52-0.48)+0.48;
w2(j1,j2)=rand*(0.52-0.48)+0.48;
end
end
figure(1)
plot(x1,x2,'.b')
hold on
plot(w1,w2,'or')
plot(w1,w2,'k','linewidth',2)
plot(w1',w2','k','linewidth',2)
hold off
title('t=0');
drawnow
no=1;
do=5;
T=300;
t=1;
while (t<=T)
n=no*(1-t/T);
d=round(do*(1-t/T));
%loop for the 1000 inputs
for i=1:1000
e_norm=(x1(i)-w1).^2+(x2(i)-w2).^2;
minj1=1;minj2=1;
min_norm=e_norm(minj1,minj2);
for j1=1:10
for j2=1:10
if e_norm(j1,j2)<min_norm
min_norm=e_norm(j1,j2);
minj1=j1;
minj2=j2;
end
end
end
j1star= minj1;
j2star= minj2;
%update the winning neuron
w1(j1star,j2star)=w1(j1star,j2star)+n*(x1(i)- w1(j1star,j2star));
w2(j1star,j2star)=w2(j1star,j2star)+n*(x2(i)- w2(j1star,j2star));
%update the neighbour neurons
for dd=1:1:d
jj1=j1star-dd;
jj2=j2star;
if (jj1>=1)
w1(jj1,jj2)=w1(jj1,jj2)+n*(x1(i)-w1(jj1,jj2));
w2(jj1,jj2)=w2(jj1,jj2)+n*(x2(i)-w2(jj1,jj2));
end
jj1=j1star+dd;
jj2=j2star;
if (jj1<=10)
w1(jj1,jj2)=w1(jj1,jj2)+n*(x1(i)-w1(jj1,jj2));
w2(jj1,jj2)=w2(jj1,jj2)+n*(x2(i)-w2(jj1,jj2));
end
jj1=j1star;
jj2=j2star-dd;
if (jj2>=1)
w1(jj1,jj2)=w1(jj1,jj2)+n*(x1(i)-w1(jj1,jj2));
w2(jj1,jj2)=w2(jj1,jj2)+n*(x2(i)-w2(jj1,jj2));
end
jj1=j1star;
jj2=j2star+dd;
if (jj2<=10)
w1(jj1,jj2)=w1(jj1,jj2)+n*(x1(i)-w1(jj1,jj2));
w2(jj1,jj2)=w2(jj1,jj2)+n*(x2(i)-w2(jj1,jj2));
end
end
end
t=t+1;
figure(1)
plot(x1,x2,'.b')
hold on
plot(w1,w2,'or')
plot(w1,w2,'k','linewidth',2)
plot(w1',w2','k','linewidth',2)
hold off
title(['t=' num2str(t)]);
drawnow
end

댓글 수: 2

i want explaination of this code
Contact Ammar AL-Jodah and ask him to explain it, and put in a lot more comments like any good professional programmer would have done. There is no excuse for bad code like that.

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

카테고리

도움말 센터File Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

질문:

2017년 10월 15일

댓글:

2022년 7월 21일

Community Treasure Hunt

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

Start Hunting!

Translated by