How I can make linked array with matlab?

Recently, I try to make multiple(?) linked array.
for example,
idxA = [1, 2, 3, 4, 5] idxB = [6, 7, 8]
I would like to build new array which can represent linked information between idxB and idxA. idxB elements can linked more than one idxA.
6|-> 1, 7|->4, 7|->1, 8|->2, 8\->5, 8|->3, ...
Finally, I would like to get array as below
result = [1, [4,1], [2,5,3]]
How I can make it.
I hope that many of you will participate. Thank you.

 採用された回答

Guillaume
Guillaume 2015 年 3 月 16 日
編集済み: Guillaume 2015 年 3 月 16 日

1 投票

'linked array' does not mean anything in matlab or any language that I know. Maybe you mean linked list, matlab does not have that.
Anyway, your mapping between A and B can be simply defined by a two-column matrix:
link = [6 1
7 4
7 1
8 2
8 5
8 3]; %doesn't even have to be ordered
Your result would be have to be a cell array, which you can create any number of ways. The shortest (but maybe not the easiest to understand if you're not familiar with accumarray) would be:
idxB = [6 7 8];
link = [6 1; 7 4; 7 1; 8 2; 8 5; 8 3];
[~, Brow] = ismember(link(:, 1), idxB);
result = accumarray(Brow, link(:, 2), [], @(v) {v'})';
celldisp(result)
Another way, not using accumarray or ismember:
idxB = [6 7 8];
link = [6 1; 7 4; 7 1; 8 2; 8 5; 8 3];
result = cell(size(idxB));
for idx = 1:numel(idxB)
result{idx} = link(link(:, 1) == idxB(idx), 2)';
end
celldisp(result)

1 件のコメント

Heejun Yoo
Heejun Yoo 2015 年 3 月 17 日
編集済み: Heejun Yoo 2015 年 3 月 17 日
Thank you very much for your assistance.

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeMatrices and Arrays についてさらに検索

タグ

タグが未入力です。

質問済み:

2015 年 3 月 16 日

編集済み:

2015 年 3 月 17 日

Community Treasure Hunt

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

Start Hunting!

Translated by