How do I create an exact looping code for my following problem?
Infos
Cette question est clôturée. Rouvrir pour modifier ou répondre.
Afficher commentaires plus anciens
I have the following mathlab code
clc;clear;close all;
c=[10 2 20 11
12 7 9 20
4 14 16 18];
s=[15
25
10
];
d=[5 15 15 15];
[m,n]=size(c);
phi = 1:m*n
pop_size=25
for ii=1:pop_size
ii
for count = 1 : length(phi)
used_idx = randi(length(phi))
trial = phi(used_idx)
phi(used_idx) = []
i=[1+mod((trial-1),m)]
j=[1+mod((trial-1),n)]
x(i,j)=min(s(i),d(j))
s(i)= s(i)-x(i,j)
d(j)= d(j)-x(i,j)
end
end
pop_size: 25
the first iteration
the result
i =
j =
x (i, j)
s (i)
d (j)
there is
however
iteration 2
doesn't appear again
can anyone provide a solution regarding the loop code that I use so that iteration 1 to iteration 25 all values appear?
thank you
1 commentaire
Rik
le 6 Oct 2020
Please stop posting new questions if you're asking the same question in a comment. If someone sees the new thread but not the old, that results in a waste of time.
Réponses (1)
Rik
le 5 Oct 2020
0 votes
You are remove all elements from phi, so your second iteration does what you ask: nothing.
Please learn how to use the debugging tools. If you had stepped through your code line by line you would have found this issue yourself.
17 commentaires
Muhammad Sam'an
le 6 Oct 2020
Rik
le 6 Oct 2020
Make a copy of phi that you can restore every iteration.
Muhammad Sam'an
le 6 Oct 2020
Rik
le 6 Oct 2020
Typos matter a lot: you were probably looking for repmat instead. But you don't need it:
newphi=phi;
Muhammad Sam'an
le 6 Oct 2020
Modifié(e) : Rik
le 6 Oct 2020
Rik
le 6 Oct 2020
You don't need repmat. Where in your code are you using newphi=phi;?
Walter Roberson
le 6 Oct 2020
Move
phi = 1:m*n
to just before the line
for i=1:length(phi)
Muhammad Sam'an
le 6 Oct 2020
Rik
le 6 Oct 2020
As Walter suggested:
clc;clear;
c=[10 2 20 11
12 7 9 20
4 14 16 18];
s=[15
25
10
];
d=[5 15 15 15];
[m,n]=size(c);
pop_size=25;
for ii=1:pop_size
fprintf('ii=%d\n',ii);
phi = 1:m*n;
for i=1:length(phi)
used_idx = randi(length(phi));
trial = phi(used_idx);
phi(used_idx) = [];
i=[1+mod((trial-1),m)];
j=[1+mod((trial-1),n)];
x(i,j)=min(s(i),d(j));
s(i)= s(i)-x(i,j);
d(j)= d(j)-x(i,j);
end
end
Muhammad Sam'an
le 6 Oct 2020
Rik
le 6 Oct 2020
The mind reading toolbox hasn't been published, so you will have to explain what you want, not just give incomplete statements that leave us guessing.
There is no way a value of 0 will be assigned to x, so how do think that value gets there? Look at all the lines where x occurs:
x(i,j)=min(s(i),d(j));
s(i)= s(i)-x(i,j);
d(j)= d(j)-x(i,j);
Only the first one is relevant here, because that is the only place where you assign a value.
What values are possible? Well, it will be an element of s or an element of d, so at least 5. So how does it become 0 in some places? Use the debugging tools. Put a breakpoint at the first line and step through your code line by line. You are ignoring the warning that mlint is giving you, as well as my previous advice to use the debugging tools. You should ignore at most one of them.
Walter Roberson
le 6 Oct 2020
s(i)= s(i)-x(i,j);
You are not resetting s every for ii, so once you have assigned 0 into s(i) once, then the ntext time around, min() of that 0 and anything else is going to be 0.
The way your code is structured, each iteration of for i, you assign a 0 into either s or d or both. You never reset s or d so those 0 are there to stay.
Muhammad Sam'an
le 6 Oct 2020
Walter Roberson
le 6 Oct 2020
Move your assignments to s and d to inside your
for ii=1:pop_size
Muhammad Sam'an
le 6 Oct 2020
Walter Roberson
le 6 Oct 2020
Modifié(e) : Walter Roberson
le 6 Oct 2020
c = [10 2 20 11
12 7 9 20
4 14 16 18];
[m,n] = size(c);
pop_size = 25
for ii = 1:pop_size
ii
phi = 1:m*n;
s = [15
25
10
];
d = [5 15 15 15];
for i=1:length(phi)
used_idx = randi(length(phi));
trial = phi(used_idx);
phi(used_idx) = [];
i = [1+mod((trial-1),m)];
j = [1+mod((trial-1),n)];
x(i,j) = min(s(i),d(j));
s(i) = s(i)-x(i,j);
d(j) = d(j)-x(i,j);
end
disp(x)
disp(s)
disp(d)
end
Muhammad Sam'an
le 6 Oct 2020
Cette question est clôturée.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!