Prev: Simscape power converter model
Next: lsqnonlin
From: Namo Namo on 15 Jul 2010 22:40 I have two vectors -- one contains the values, and the other is the corresponding index. For example A = [0.1 0.3 0.2 0.5 .... ] B = [1 3 4 2 1 ] The have the same length. I need to select the elements in A with the same index in B, and sum them. For each index in B, the numbers of the corresponding elements in A are not the same. So I cannot just sort and reshape and sum, etc. I can do a for loop S = zeros(length(B),1); for k =1:length(B) S(k) = sum( A(B==k) ); end But for large A and B, this takes too long. Any advice is welcome.
From: Roger Stafford on 15 Jul 2010 22:51 "Namo Namo" <wynamo(a)yahoo.com> wrote in message <i1ogq8$9j7$1(a)fred.mathworks.com>... > I have two vectors -- one contains the values, and the other is the corresponding index. For example > > A = [0.1 0.3 0.2 0.5 .... ] > B = [1 3 4 2 1 ] > > The have the same length. I need to select the elements in A with the same index in B, and sum them. For each index in B, the numbers of the corresponding elements in A are not the same. So I cannot just sort and reshape and sum, etc. > > I can do a for loop > > S = zeros(length(B),1); > for k =1:length(B) > S(k) = sum( A(B==k) ); > end > > But for large A and B, this takes too long. Any advice is welcome. - - - - - - - - S = sum(A(B)); Note: A and B don't have to be the same length for this to work. However the indices in B must all lie within the range 1:length(A). Roger Stafford
From: Walter Roberson on 15 Jul 2010 23:04 Namo Namo wrote: > I have two vectors -- one contains the values, and the other is the > corresponding index. For example > > A = [0.1 0.3 0.2 0.5 .... ] > B = [1 3 4 2 1 ] > > The have the same length. I need to select the elements in A with the > same index in B, and sum them. Consider using accumarray()
From: Namo Namo on 15 Jul 2010 23:09 I just tried this with the simple example >> A = [0.1 0.3 0.2 0.5 0.2 0.9]; >> B = [1 3 4 2 1 4 ]; >> sum(A(B)) ans = 1.7000 This is not what I want. I may have made a typo in my previous post. What I want is to sum all A elements corresponding to 1 in B, 2 in B, etc. So here since there are 1 to 4 in B, I expect to get 4 numbers sum(A(B==1)) ... sum(A(B==4)) for example, A(B==4) = [0.2 0.9], and the sum is 1.1 > - - - - - - - - > S = sum(A(B)); > > Note: A and B don't have to be the same length for this to work. However the indices in B must all lie within the range 1:length(A). > > Roger Stafford
From: Roger Stafford on 15 Jul 2010 23:09
"Roger Stafford" <ellieandrogerxyzzy(a)mindspring.com.invalid> wrote in message <i1ohen$jid$1(a)fred.mathworks.com>... > "Namo Namo" <wynamo(a)yahoo.com> wrote in message <i1ogq8$9j7$1(a)fred.mathworks.com>... > > I have two vectors -- one contains the values, and the other is the corresponding index. For example > > > > A = [0.1 0.3 0.2 0.5 .... ] > > B = [1 3 4 2 1 ] > > > > The have the same length. I need to select the elements in A with the same index in B, and sum them. For each index in B, the numbers of the corresponding elements in A are not the same. So I cannot just sort and reshape and sum, etc. > > > > I can do a for loop > > > > S = zeros(length(B),1); > > for k =1:length(B) > > S(k) = sum( A(B==k) ); > > end > > > > But for large A and B, this takes too long. Any advice is welcome. > - - - - - - - - > S = sum(A(B)); > > Note: A and B don't have to be the same length for this to work. However the indices in B must all lie within the range 1:length(A). > > Roger Stafford - - - - - - - I didn't read your code carefully enough, Namo. Forget what I sent earlier. The following might do the job: S = accumarray(B,A(B)); Roger Stafford |