Azzera filtri
Azzera filtri

How to put Multiple Parallel Entries in Vectors?

1 visualizzazione (ultimi 30 giorni)
I have q=zeros(20,1) and x=[3;4;5;6;7;8;9;1;2;3;4;5]
If I declare q(x,1)=1 then I get:
q=[1;1;1;1;1;1;1;1;1;0;0;0;0;0;0;0;0;0;0;0].
And if later I apply q(x,1)=q(x,1)+1 then I get:
q=[2;2;2;2;2;2;2;2;2;0;0;0;0;0;0;0;0;0;0;0].
So, using only MATLAB, how can I get with q(x,1)=q(x,1)+1 this result:
q=[2;2;3;3;3;2;2;2;2;0;0;0;0;0;0;0;0;0;0;0] ??
-Ele

Risposta accettata

Walter Roberson
Walter Roberson il 21 Dic 2012
q = q + accumarray(x, 1, size(q));

Più risposte (0)

Categorie

Scopri di più su Software Development Tools in Help Center e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by