Effacer les filtres
Effacer les filtres

error " Assignment has fewer non singleton rhs dimensions than non singleton subscripts"

1 vue (au cours des 30 derniers jours)
Hello, I have 2 arrays that represent indexes of a matrix, for example idx =[ 1 5 ] and idy = [ 2 3] . When I execute this line
M(idx,idy)=1; % M is a matrix
all the components M(1,2) M(1,3) M(5,2) and M(5,3) are affected . What can I do so that only M(1,2) and M(5,3) become equal to 1 ? ( without any loop)
N.B : I used sub2ind (size(M),idx,idy) but it's slow in my case ( not this example) because the matrix M is bigger.
Thank you in advance

Réponse acceptée

Stephen23
Stephen23 le 1 Juin 2017
Modifié(e) : Stephen23 le 1 Juin 2017
Use sub2ind:
M(sub2ind(size(M),idx,idy)) = 1
@Elkhanssaa Marsali: you just edited your question to add this line "I used sub2ind (size(M),idx,idy) but it's slow in my case"
Well, using sub2ind is one solution, otherwise you could use a loop. It does not matter if you want a faster solution, those are the solutions for your problem. Well, I guess this is one answer that will never get accepted :)
  2 commentaires
John D'Errico
John D'Errico le 1 Juin 2017
sub2ind is the correct solution for the problem as posed.
I see it was added that sub2ind was too slow. That suggests that the OP is either using a sparse matrix and trying to add elements (which will be SLOW, DON'T do it that way!) or they are growing the matrix over time, adding elements outside the current range of indexes. DON'T do that either!
So don't grow matrices. (There are tricks to avoid this.) Don't stuff elements into a sparse matrix. (Instead, learn to use sparse matrix tools.)
The question of why sub2ind was too slow depends on what they are doing.
emar
emar le 2 Juin 2017
Hmm , ok I will change the type of data

Connectez-vous pour commenter.

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