Hello.
I've a matrix 61312*3.
1st column: from 1 to 81 (Originating Place)
2nd column: from 1 to 81 (Destination place of a vehicle)
3rd column: from 1 to 246 (where the vehicle has surveyed)
There are 61312 observation (no. of rows) at 246 locations.
I need to create a matrix, which shows that from origin (1 to 81) to Destination (1 to 81), which of the location (1 to 246) these vehicle follows. I think the order of the matrix will be 81*81, but there can be more than one value in a cell because of many surveyed location.
With the help of this forum, I've done below coding.
A=combinedfile;
[a,~,ii] = unique(A(:,1));
[b,~,jj] = unique(A(:,2));
out1 = accumarray([ii,jj],A(:,3),[max(ii), max(jj)],@(x){x});
out = [[{nan};num2cell(a(:))],[num2cell(b(:)');out1]];
I get the matrix at the end, but the problems are some cells contains brackets while some contain vaules*1 double (i.e. 440*1 double).
Kindly let me know how should I get rid of these problems. Thanks in advance.

 Réponse acceptée

Andrei Bobrov
Andrei Bobrov le 6 Juil 2014
Modifié(e) : Andrei Bobrov le 6 Juil 2014

1 vote

use
[a,~,ii] = unique(A(:,1));
[b,~,jj] = unique(A(:,2));
f = @(x)x(1); % or f = @(x)x(end); or f = @(x)x(randperm(numel(x),1));
out1 = accumarray([ii,jj],A(:,3),[max(ii), max(jj)],f);
out = [nan,b(:)';a(:),out1];

4 commentaires

Fayyaz
Fayyaz le 6 Juil 2014
Hi Andrei, thanks for this.
Now the other problems solved, but this time I get only one value in the cell in the matrix, but there can be more than one values in the cell, because the vehicles can go through many locations (through 1 to 246) from Origin (1 to 81) to Destination (1 to 81)
Andrei Bobrov
Andrei Bobrov le 6 Juil 2014
You want 3d matrix?
Fayyaz
Fayyaz le 6 Juil 2014
Modifié(e) : Fayyaz le 6 Juil 2014
I want a matrix 81*81, but there should be multiple values in cells i.e. from Origin (1*81) to Destination (1*81), a vehicle can go through multiple locations i.e. (1 to 246). I don't know whether it should be 3d or not.
Fayyaz
Fayyaz le 6 Juil 2014
@Andrei, the previous code was perfect, except instead of brackets there should be zero, and instead of 440*1 double, it should be 1.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Graphics Performance dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by