필터 지우기
필터 지우기

Replacing elements of a matrix with vectors

조회 수: 3 (최근 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 CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by