Need algorithm to create the list

Hello,
I need to create a matrix of order 1614006*3, The sequence should be
1st Col (origin) , 2nd Col (Destination) , 3rd Col (Survey_Location)
Origin (1) Destination(1) Survey_Location(1)
Origin (1) Destination(1) Survey_Location(2)
.......
Origin (1) Destination(1) Survey_Location(246)
Origin (1) Destination(2) Survey_Location(1)
...........
Origin (1) Destination(81) Survey_Location(246)
......
Origin (2) Destination(1) Survey_Location(1)
.........
Origin (81) Destination(81) Survey_Location(246)
For this case rows should be 81*81*246=1614006

2 comentarios

Star Strider
Star Strider el 6 de Jul. de 2014
How does this differ from your earlier question: Survey data Matrix arrangement?
Fayyaz
Fayyaz el 6 de Jul. de 2014
Hello, it is not different from the previous question. I just posted this question in a more clear way. I am going to delete the previous question.
Thanks.

Iniciar sesión para comentar.

 Respuesta aceptada

Image Analyst
Image Analyst el 6 de Jul. de 2014
Editada: Image Analyst el 6 de Jul. de 2014
I'm sure there's a more clever vectorized way - Andrei can probably do it in a single line! - but there's always the brute force way:
Origin = randi(9, [81, 1]);
Destination = randi(9, [81, 1]);
Survey_Location = randi(9, [246, 1]);
numOutputRows = size(Origin, 1) * size(Destination, 1) * size(Survey_Location, 1)
output = zeros(numOutputRows, 3);
tic;
row = 1;
for r1 = 1 : size(Origin, 1)
for r2 = 1 : size(Destination, 1)
for r3 = 1 : size(Survey_Location, 1)
output(row, :) = [Origin(r1), Destination(r2), Survey_Location(r3)];
row = row + 1;
end
end
end
toc
Takes 1.4 second on my computer.

5 comentarios

Fayyaz
Fayyaz el 6 de Jul. de 2014
@Image Analyst: I don't know whether it's too heavy or other reason, it takes a lot of time to run, till now it's in running mode (Although I've i-7 computer)
Also, have you created 3 matrices for Origin (81*1), Destination (81*1) and Survey_Location(246*1)??
Image Analyst
Image Analyst el 6 de Jul. de 2014
Editada: Image Analyst el 6 de Jul. de 2014
Yes. I did. I'll edit to add those before the tic. You have a faster computer than me. I'm using an i5 at the moment. You should get it done at least as fast as me. Actually Cedric's method is probably going to be faster, though perhaps a little more cryptic.
Fayyaz
Fayyaz el 6 de Jul. de 2014
@Image Analyst: Now it's working. But there is one small problem.
I have origin and destination values from 1 to 81, and Survey_Location values from 1 to 246.
In the output, I can't find values greater than 10. Probably you've defined the these in other manner.
Kindly define these vectors as a sequence from 1 to 81 (by 1 increment) for Origin and Destination, and 1 to 246 (by 1 increment) for Survey_Location.
Thank you very much.
Oh, I thought you already had some arrays so I just used sample arrays. If you want sequential arrays, you can do
Origin = (1:81)';
Destination = (1:81)';
Survey_Location = (1:246)';
Fayyaz
Fayyaz el 6 de Jul. de 2014
Perfect! Actually I am not that much familiar with MATLAB, but I hope I will be able to understand quite soon. Thanks again.

Iniciar sesión para comentar.

Más respuestas (2)

Andrei Bobrov
Andrei Bobrov el 6 de Jul. de 2014
[z,y,x] = ndgrid(Survey_Location,Destination,Origin);
out = [x(:),y(:),z(:)];

3 comentarios

Fayyaz
Fayyaz el 6 de Jul. de 2014
Thanks Andrei. It has worked very well.
Image Analyst
Image Analyst el 6 de Jul. de 2014
Aha! I knew Andrei could do it in a line or two. He's the master of that.
Fayyaz
Fayyaz el 6 de Jul. de 2014
Ya I've realized when Andrei did this.
@Andrei, if possible, kindly look into this question too, you've almost solved it.

Iniciar sesión para comentar.

Cedric
Cedric el 6 de Jul. de 2014
Editada: Cedric el 6 de Jul. de 2014
Hello Muhammad,
Here is an example, with 3 elements Origin and Destination, and a 4 elements Survey_Location..
% - Dummy test case.
Origin = 10 : 12 ;
Destination = 100 : 102 ;
Survey_Location = [500, 600, 700, 800] ;
% - Define columns of final array and concatenate.
c1 = reshape( repmat( Origin(:)', 3*4, 1 ), [], 1 ) ;
c2 = repmat( reshape( repmat( Destination(:)', 4, 1 ), [], 1 ), 3, 1 ) ;
c3 = repmat( Survey_Location(:), 3*3, 1 ) ;
X = [c1, c2, c3] ;
Running this, you get
>> X
X =
10 100 500
10 100 600
10 100 700
10 100 800
10 101 500
10 101 600
10 101 700
10 101 800
10 102 500
10 102 600
10 102 700
10 102 800
11 100 500
11 100 600
11 100 700
11 100 800
11 101 500
11 101 600
11 101 700
11 101 800
11 102 500
11 102 600
11 102 700
11 102 800
12 100 500
12 100 600
12 100 700
12 100 800
12 101 500
12 101 600
12 101 700
12 101 800
12 102 500
12 102 600
12 102 700
12 102 800
EDIT : for your case, you want to replace 3 by 81, and 4 by 246.

Categorías

Más información sobre Creating and Concatenating Matrices en Centro de ayuda y File Exchange.

Preguntada:

el 6 de Jul. de 2014

Comentada:

el 6 de Jul. de 2014

Community Treasure Hunt

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

Start Hunting!

Translated by