Searching and summing array

조회 수: 2 (최근 30일)
Fischer Zheng
Fischer Zheng 2015년 10월 30일
답변: Fischer Zheng 2015년 11월 2일
Hi All, I have three arrays. One small one called A and two huge ones called M1 and M2. M1 and M2 are of the same size.
A = ['K1','K2','K3'....];
M1 = ['K9','K45','K2'..];
M2 = [123,34.2,1654,...];
I am trying to sum all the values in M2 base on M1 into the buckets in A. As the current example, 'K2' bucket would have 1654 plus anything else that is 'K2' in M1.
Thanks in advance, Fischer
  댓글 수: 1
Jan
Jan 2015년 10월 30일
Note, that ['K1','K2','K3'] is the string 'K1K2K3'. Is this really the representation in your code? Or is A a cell string?

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

채택된 답변

Fischer Zheng
Fischer Zheng 2015년 11월 2일
This is what did on my first try and it takes 30mins to run through the code. However, when I did two for loops it is much faster. Taking only 15 seconds.
I guess the reason is that there are usually only two maybe three elements per key. running using vectorization is much slower because it go through the big array too many times

추가 답변 (1개)

Geoff Hayes
Geoff Hayes 2015년 10월 30일
Fischer - think about how you would do this by hand. You would start with the first element of A, 'K1', and then look at each element of M1. Every match you find you would then add to the sum corresponding to 'K1'. Try doing something like the following where we assume that A and M1 are cell arrays and we use find to determine the indices of those elements of M1 that match the element of A that we are interested in.
% create an array that will manage the sums for each element of A
sumsOfA = zeros(size(A));
% iterate over each element of A
for k=1:length(A)
% get the indices of M1 that match A{k}
idcs = find(strcmp(A{k},M1))
% sum the elements of M1
sumsOfA(k) = sum(M2(idcs));
end
  댓글 수: 2
Jan
Jan 2015년 10월 30일
This works faster without the find command.
Geoff Hayes
Geoff Hayes 2015년 10월 31일
For sure! The result of
strcmp(A{k},M1)
will return an array of zeros and ones which can the be used to access the elements of M2.
Thanks, Jan!

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

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by