フィルターのクリア

Matrix which cells have multiple values

1 回表示 (過去 30 日間)
Fayyaz
Fayyaz 2014 年 7 月 10 日
コメント済み: Jos (10584) 2014 年 7 月 11 日
(This question was asked previously, but I didn't explain very well, and this time I attached the data file too for more explanation)
Hello
A=Survey.txt;
I've a matrix 61312*3.
1st column: values from 1 to 246 (location, where the vehicle has surveyed)
2nd column: values from 1 to 81 (Originating Place)
3rd column: values from 1 to 81 (Destination place of a vehicle)
I need to have a matrix 81*81 (origin(2nd col)*destination(3rd col)), which every cell may contain multiple values (i.e. the location of the survey(values from 1st col)) because this survey has done in 5 years, so there can be 5 different values(or less or zero) in each cell.
Andrei Bobrov had done the below coding, as a result I get multiple values for most of the cells of matrix, but the problem is some cells contains brackets, while some shows number*1 double etc.
[a,~,ii] = unique(A(:,2)); [b,~,jj] = unique(A(:,3)); out1 = accumarray([ii,jj],A(:,1),[max(ii), max(jj)],@(x){x}); out = [[{nan};num2cell(a(:))],[num2cell(b(:)');out1]];
  1 件のコメント
Jan
Jan 2014 年 7 月 10 日
Please use the "{} Code" button to format your code.

サインインしてコメントする。

採用された回答

Jos (10584)
Jos (10584) 2014 年 7 月 11 日
This can be done with a simple one-liner:
M = accumarray(A(:,[2 3]), A(:,1), [81 81], @(x) {x})
M will be a 81-by-81 cell array where M(I,J) contains the survey locations of vehicles going from I to J
Read the help of accumarray, and note the example of "Group values in a cell array".
  2 件のコメント
Fayyaz
Fayyaz 2014 年 7 月 11 日
@Jos
Can you tell me how I can export this matrix to Excel?
I was trying but couldn't.
Jos (10584)
Jos (10584) 2014 年 7 月 11 日
Sorry, I have never used excel ;-)

サインインしてコメントする。

その他の回答 (1 件)

Jan
Jan 2014 年 7 月 10 日
The cell does not contain brackets. These brackets are shown only inthe command window for an empty matrix. Therefore I assume, that the posted code works well.

カテゴリ

Help Center および File ExchangeStructures についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by