How do I create an exact looping code for my following problem?
信息
此问题已关闭。 请重新打开它进行编辑或回答。
显示 更早的评论
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 个评论
Rik
2020-10-6
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.
回答(1 个)
Rik
2020-10-5
0 个投票
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 个评论
Muhammad Sam'an
2020-10-6
Rik
2020-10-6
Make a copy of phi that you can restore every iteration.
Muhammad Sam'an
2020-10-6
Rik
2020-10-6
Typos matter a lot: you were probably looking for repmat instead. But you don't need it:
newphi=phi;
Muhammad Sam'an
2020-10-6
编辑:Rik
2020-10-6
Rik
2020-10-6
You don't need repmat. Where in your code are you using newphi=phi;?
Walter Roberson
2020-10-6
Move
phi = 1:m*n
to just before the line
for i=1:length(phi)
Muhammad Sam'an
2020-10-6
Rik
2020-10-6
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
2020-10-6
Rik
2020-10-6
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
2020-10-6
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
2020-10-6
Walter Roberson
2020-10-6
Move your assignments to s and d to inside your
for ii=1:pop_size
Muhammad Sam'an
2020-10-6
Walter Roberson
2020-10-6
编辑:Walter Roberson
2020-10-6
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
2020-10-6
此问题已关闭。
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!