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

조회 수: 5 (최근 30일)
Pete
Pete 2013년 3월 24일
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
Image Analyst
Image Analyst 2013년 3월 24일
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 2013년 3월 24일
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

댓글을 달려면 로그인하십시오.

채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2013년 3월 24일
편집: Azzi Abdelmalek 2013년 3월 24일
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
Pete
Pete 2013년 3월 24일
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 2013년 3월 24일
Sorry that was not a good question, simply swap the sum for prod! Thanks again!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Cedric
Cedric 2013년 3월 24일
편집: Cedric 2013년 3월 24일
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
Cedric
Cedric 2013년 3월 25일
편집: Cedric 2013년 3월 25일
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 2013년 3월 25일
편집: Azzi Abdelmalek 2013년 3월 25일
Exact, I was wondering what was wrong.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by