Effacer les filtres
Effacer les filtres

Replacing elements of a matrix with vectors

3 vues (au cours des 30 derniers jours)
Robert Mason
Robert Mason le 8 Août 2016
Modifié(e) : Stephen23 le 8 Août 2016
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?

Réponse acceptée

Stephen23
Stephen23 le 8 Août 2016
Modifié(e) : Stephen23 le 8 Août 2016
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

Plus de réponses (0)

Catégories

En savoir plus sur Matrices and Arrays dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by