Assign a Sub Array to Array Knowing the Number of Dimensions at Run Time
5 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Royi Avital
le 10 Fév 2022
Commenté : Royi Avital
le 11 Fév 2022
Assume we have tA and tB with the same number of dimesions. We also have all(size(tB) <= size(tA)) == true.
The task is to embed tB in tA. Something like: tA(1:size(tB, 1), 1:size(tB, 2), ..., 1:size(tB, n)) = tB. Yet since we know the number of dimensions only at runtime, it can't be written explicitly like that.
The question, is there an efficient way to do so without eval or explicitly computer the cartesian product and use sub2ind()?
0 commentaires
Réponse acceptée
Royi Avital
le 10 Fév 2022
Modifié(e) : Royi Avital
le 11 Fév 2022
Plus de réponses (1)
Rik
le 10 Fév 2022
There is probably a better way, but you can fill a cell array with the index vectors (simple loop with ndims), and then use this:
tA(inds{:}) = tB;
0 commentaires
Voir également
Catégories
En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!