フィルターのクリア

Replacing elements of a matrix with vectors

2 ビュー (過去 30 日間)
Robert Mason
Robert Mason 2016 年 8 月 8 日
編集済み: Stephen23 2016 年 8 月 8 日
I have a matrix of integers, for instance:
[1 2; 3 4], and I would like to replace each integer with a, for instance:
1 by [1 2 1], 2 by [2 7 2], 3 by [6 4 3], 4 by [9 8 2], so that the final matrix is:
[1 2 1 2 7 2; 6 4 3 9 8 2]
Any suggestions for how to do this?

採用された回答

Stephen23
Stephen23 2016 年 8 月 8 日
編集済み: Stephen23 2016 年 8 月 8 日
Simply put the replacement arrays inside a cell array and then use the starting matrix as indices:
>> mat = [1,2;3,4];
>> rep = {[1,2,1],[2,7,2],[6,4,3],[9,8,2]};
>> cell2mat(rep(mat))
ans =
1 2 1 2 7 2
6 4 3 9 8 2

その他の回答 (0 件)

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by