Borrar filtros
Borrar filtros

Can you split a vector according to a pre-defined sequence?

3 visualizaciones (últimos 30 días)
Can anyone suggest a way in which it is possible to perform operations on a vector according to a predetermined sequence - for example I have a vector of different values, M, which is < 8760x1 > in size. I have another vector with a sequence of numbers, P, (size < 300x1 >) and this sequence sums to 8760. I would like to use these P values to index the vector M and find the product of each index.
An example to make this clearer:
M = [1,2,4,2,3,4,5,3,4,2];
P = [2,2,4,2];
Result = [3,6,15,6]
Any help here would be greatly appreciated.
Peter.S.
  3 comentarios
Image Analyst
Image Analyst el 24 de Mzo. de 2013
Can you explain how you got Result? Because I don't understand. Using P as indexes of M gives [M(2), M(2), M(4), M(2)], and the product of all those i = 2*2*2*2 which equals 16, not 3 (which is Result(1)). Why do you have 4 Result elements and not just 1? On the other hand "the product of each index." would be 2*2*4*2 which is the product of all the indexes (the numbers in P), and that is 32 - again, not 3 and not [3,6,15,6].
Pete
Pete el 24 de Mzo. de 2013
Sorry I'm not sure which 6 you are referring to -
Result gives the value of M(1,1)+M(1,2) =3, then M(1,3)+M(1,4) =6, then M(1,5)+M(1,6)+M(1,7)+M(1,8)=15 and M(1,9)+ M(1,10) =6

Iniciar sesión para comentar.

Respuesta aceptada

Azzi Abdelmalek
Azzi Abdelmalek el 24 de Mzo. de 2013
Editada: Azzi Abdelmalek el 24 de Mzo. de 2013
M = [1,2,4,2,3,4,5,3,4,2];
P = [2,2,4,2];
id2=cumsum(P);
id1=[1 id2(1:end-1)+1];
for k=1:numel(id1)
Result(k)=sum(M(id1(k):id2(k)));
end
%or
id2=cumsum(P);
id1=[1 id2(1:end-1)+1];
Result=arrayfun(@(x,y) sum(M(x:y)),id1,id2)
  2 comentarios
Pete
Pete el 24 de Mzo. de 2013
Thankyou that does work perfectly. I also need the product of these values i.e. so the Result would now be [2,8,180,8]. Is there a similar method to achieve this?
Thanks again for your help.
Pete
Pete el 24 de Mzo. de 2013
Sorry that was not a good question, simply swap the sum for prod! Thanks again!

Iniciar sesión para comentar.

Más respuestas (1)

Cedric
Cedric el 24 de Mzo. de 2013
Editada: Cedric el 24 de Mzo. de 2013
id = zeros(numel(M), 1) ;
id(1+cumsum(P)) = 1 ;
id = 1 + cumsum(id(1:end-1)) ;
result = accumarray(id, M(:)).' ;
With the vectors M and P from your statement, this code leads to:
result =
3 6 15 6
Note that for small sizes of M and P, Azzi's solution is much more efficient than mine, and my solution becomes more efficient than his when sizes become significant.
  3 comentarios
Cedric
Cedric el 25 de Mzo. de 2013
Editada: Cedric el 25 de Mzo. de 2013
Hello Azzi, yes, in your test sum(P) is not equal to length(M).
I performed my tests on larger arrays with
n = 1000 ; % 1 for original setup.
M = repmat([1,2,4,2,3,4,5,3,4,2], 1, n) ;
P = repmat([2,2,4,2], 1, n) ;
tic ;
result_AA1 = zeros(size(P)) ;
idx=cumsum(P); ii=1; jj=P(1);
for k=1:numel(idx)
jj=P(k)+ii-1;
result_AA1(k)=sum(M(ii:jj));
ii=jj+1;
end
toc
tic ;
id2=cumsum(P);
id1=[1 id2(1:end-1)+1];
result_AA2 = arrayfun(@(x,y) sum(M(x:y)),id1,id2) ;
toc
tic ;
id = zeros(numel(M), 1) ;
id(1+cumsum(P)) = 1 ;
id = 1 + cumsum(id(1:end-1)) ;
result_CW = accumarray(id, M(:)).' ;
toc
all(result_AA2 == result_AA1 & result_CW == result_AA1)
And I obtained
Elapsed time is 0.010770 seconds.
Elapsed time is 0.031326 seconds.
Elapsed time is 0.000788 seconds.
ans =
1
EDIT: for n=1, however:
Elapsed time is 0.000036 seconds.
Elapsed time is 0.001322 seconds.
Elapsed time is 0.000582 seconds.
ans =
1
Azzi Abdelmalek
Azzi Abdelmalek el 25 de Mzo. de 2013
Editada: Azzi Abdelmalek el 25 de Mzo. de 2013
Exact, I was wondering what was wrong.

Iniciar sesión para comentar.

Categorías

Más información sobre Logical en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by