I need a detailed explanation about this code
7 views (last 30 days)
Show older comments
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 Comments
Jan
on 15 Oct 2017
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
Accepted Answer
KL
on 15 Oct 2017
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?
More Answers (1)
Priyanka Akre
on 22 Apr 2020
Edited: Image Analyst
on 21 Jul 2022
% 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 Comments
Image Analyst
on 21 Jul 2022
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.
See Also
Categories
Find more on Graphics Performance 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!