Info
Diese Frage ist geschlossen. Öffnen Sie sie erneut, um sie zu bearbeiten oder zu beantworten.
How do I create an exact looping code for my following problem?
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentar
Rik
am 6 Okt. 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.
Antworten (1)
Rik
am 5 Okt. 2020
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 Kommentare
Walter Roberson
am 6 Okt. 2020
Bearbeitet: Walter Roberson
am 6 Okt. 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
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!