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 commentaire

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
Rik le 5 Oct 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 commentaires

So how to fix the mathlab code so that every iteration 1 to iteration two is the same step. please give a solution?
Make a copy of phi that you can restore every iteration.
I use the function newphi = rapmat (phi, 1,1)
but still can't ... where do I make the copy function?
Typos matter a lot: you were probably looking for repmat instead. But you don't need it:
newphi=phi;
so what is the complete solution .. I have defined newphi = phi but I still find an error?
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);
pop_size=25
phi = 1:m*n
%while pop_size
for ii=1:pop_size
ii
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
please how do I put the repmat function in my mathlab code?
You don't need repmat. Where in your code are you using newphi=phi;?
Move
phi = 1:m*n
to just before the line
for i=1:length(phi)
I need newphi after looping ii = 1: length (phi).
so basically I want to repeat pop_size times or loop 25 times looping ii = 1: length (phi)
how is the mathlab code solution?
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
if you use this, the value of x (i, j) = (0) when it shouldn't
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.
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.
yes i know,, burt i dont know how to reset the matrix x (i, j) or x (i, j) = 0 after the values of s (i) and d (j) are equal to zero and also how to return the s (i) and d (j) or s ( i) and d (j) are non-zero. I am still confused about this process? please find a solution or form the mathlab code
Move your assignments to s and d to inside your
for ii=1:pop_size
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);
pop_size=25
%while pop_size
for ii=1:pop_size
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))
end
s(i)= s(i)-x(i,j)
d(j)= d(j)-x(i,j)
end
like this ?
but when I run the results are still the same ... please for a solution?
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
thanks very much

Cette question est clôturée.

Tags

Clôturé :

le 20 Août 2021

Community Treasure Hunt

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

Start Hunting!

Translated by