fast and beautiful way to convert vector ranges to indexes
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hi there,
my question could easily be solved with a simple loop but I'm curious if there is a nice genuine matlab way of doing this: I have a vector "v1" containing 10 entries. First 5 belong together, next 2 belong together and the last 3 again (somehow). So I got a vector "v2" with
v2 = [5; 2; 3];
I want now something like
v3 = [1 1 1 1 1 2 2 3 3 3];
So I could access my v1 vector with:
v1(v3==1);
(Background for the question are different colors for each group with the plot-command.)
Thanks already in advance - I'm sure Matlab holds a nice and short way of doing this :-)
Vincent
0 commentaires
Réponse acceptée
Matt J
le 23 Juil 2014
c=cumsum(v2);
v3=zeros(1,c(end));
v3([1,c(1:end-1)])=1;
v3=cumsum(v3);
v3(end)=[],
Plus de réponses (4)
Andrei Bobrov
le 23 Juil 2014
v2 = [5; 2; 3];
ii = cumsum(v2);
v3 = zeros(ii(end),1);
v3( ii - v2 + 1) = 1;
v3 = cumsum(v3);
2 commentaires
Azzi Abdelmalek
le 23 Juil 2014
v2 = [5; 2; 3];
v3=cell2mat(arrayfun(@(x) repmat(x,1,v2(x)),1:numel(v2),'un',0))
2 commentaires
Voir également
Catégories
En savoir plus sur Matrix Indexing 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!