find the next row of an element in a matrix

19 views (last 30 days)
i have vector v
v = [4,5;1,2;1,3;1,4;2,6;5,7;4,7;3,4;1,5;2,3;7,9;3,5;8,9;1,6;8 10;5,9;2,4;6,9;2,5;1,7;4,9;3,7;2,8;7,8;4,6;1,9;3,9;5,6;2,7;6,7;1,8;3,8;4,8;5,8;2,9];
from this vector v, find the next row of an element in a matrix till all the elements in the matrix has taken once
i need to take start with the first row [ 4 5 ],
then take the second value in the started row (only when we start the iteration i = 1 case, we take the second value), and find, next in which row value 5 comes
It can come in any column first or second
if in the row, we take, if the other value has already been taken, we dont consider row eg:
[ 4 5; 5 7; then comes 4 7] but as 4 is already taken i dont take [4 7] i go to next [ 7 9 ] and get
[ 4 5; 5 7; 7 9....]
continue this till size(v,1)
out = [4 5; 5 7; 7 9; 8 9; 8 10];
un_out = unique(out,'stable');
un_out = [4 5 7 9 8 10]
then check
uv = unique(v);
uv = [ 1 2 3 4 5 6 7 8 9 10]
if i have got all the elements of uv in un_out, i can exit, else, i need to take the second row of v [ 1 2 ]
then continue as above,
taking the second value in the started row (as we start the iteration i = 1 case again), and find, next in which row value 2 comes
it can come in any column first or second
continue this till size(v,1)
so new out will be
un_out = { [4 5 7 9 8 10] [ 1 2 6 ] }
again only 3 is missing.
so add that to out, to get
un_out = { [4 5 7 9 8 10] [ 1 2 6 ] [ 3 ]} % final output
can someone help me to write the loop to find the next row of an element in a matrix till all the elements in the matrix has taken once
  2 Comments
Guillaume
Guillaume on 25 Mar 2020
Elysi, as with your previous question, I'm fairly certain that there are already efficient algorithm that do whatever it is you're trying to do and I feel that you would be better off identifying such algorithms. In my opinion, you would get better results if you explained the general task you're trying to solve rather than asking us to implement a specific algorithm you've come up with which may be the wrong solution to your general task.
Once again, it looks like you're trying to solve a routing problem which may be better solved by some graph theory algorithm.
Finding what your problem is called is probably the hardest part. I still have no idea how exactly your problem differs from the bin packing problem in your last question. Once you know what your problem is called finding an implementation of a solution would be significantly easier.
Elysi Cochin
Elysi Cochin on 25 Mar 2020
Sir Guillaume, its Clarke and Wright algorithm. I solved the initial steps in it. But when i reached this portion to find the path, i got stuck. I searched for different solutions, but could find one.
Do you have any suggestions? If so that would be of great help

Sign in to comment.

Accepted Answer

Rik
Rik on 25 Mar 2020
This probably isn't the fastest solution, but it does get the output you describe. The last loop I had to guess, so let me know if you meant something else.
v = [4,5;1,2;1,3;1,4;2,6;5,7;4,7;3,4;1,5;2,3;7,9;3,5;8,9;1,6;8 10;5,9;2,4;6,9;2,5;1,7;4,9;3,7;2,8;7,8;4,6;1,9;3,9;5,6;2,7;6,7;1,8;3,8;4,8;5,8;2,9];
outcell=cell(1);outcell_counter=0;
start_row=0;
while true
start_row=start_row+1;
out=[];
val=v(start_row,2);
r=start_row;
while true
out=[out;v(r,:)]; %#ok<AGROW>
skiprows=r;
[r,c]=find(v((skiprows+1):end,:)==val);
r=r+skiprows;%correct the row indices
c=(c-0.5)*-1+2.5;%flip 1 to 2 and 2 to 1
foundnewvalue=false;
for k=1:numel(r)%will be skipped if r is empty
if ~any(out(:)==v(r(k),c(k)))
foundnewvalue=true;
r=r(k);
val=v(r,c(k));
break%continue to next row
end
end
if ~foundnewvalue || r==(size(v,1)-1)
break%end of list is reached
end
end
out = unique(out,'stable')';%flip to row
outcell_counter=outcell_counter+1;
outcell{1,outcell_counter}=out;
if isempty(setdiff(unique(v(:)),cell2mat(outcell)))
break
end
end
for n=2:numel(outcell)
outcell{n}=setdiff(outcell{n},cell2mat(outcell(1:(n-1))));
end
clc,celldisp(outcell)

More Answers (0)

Community Treasure Hunt

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

Start Hunting!