could anyone help me how to update the array values with respect to the following arrays

A= [1.0119 1.6739;
2.8012 1.6381;
0.0752 0.1222;
1.3143 0.8189;
0.7956 0.8750;
1.8512 1.1499]
B =[0.9512 0.4314;
0.2490 0.8309;
0.3864 0.8246]
C= [ 3;
1;
2;
1;
2;
1]
I am having the above three matrices,I want to update B by considering the average values of A with respect to C.
For example,
In C i have 2nd,4th and 6th rows to be 1 which means i need to add the mentioned rows in A and find the average of it and it needs to be updated in first row of B.
in the same way,i need to add 3rd and 5th rows in A and it needs to get updated in second rows of B.
On doing this the updated B should be [1.9889 1.2023;
0.4354 0.4986;
0.3864 0.8246]
could anyone please help me on this.

Antworten (2)

B(1,:) = mean(A(C == 1,:));
B(2,:) = mean(A(C == 2,:));
B(3,:) = mean(A(C == 3,:));
Or you can put it in a for loop.
for ii = 1:3
B(ii,:) = mean(A(C == ii,:));
end
Cheers Alaster

3 Kommentare

I tried your code with respect to the following matrix
A= [1.0119 1.6739
2.8012 1.6381
-0.0752 0.1222
1.3143 -0.8189
-0.7956 0.8750
1.8512 1.1499]
B =[0.9512 0.4314
0.2490 0.8309
0.3864 0.8246]
C =[ 3;
1;
2;
1;
2;
1];
for ii = 1:3
B(ii,:) = mean(A(C == ii,:))
end
when i run the code i am getting the following result
B =[1.9889 0.6564
-0.4354 0.4986
1.3429 1.3429]
where the last row gets changed but it should have the same values as 0.3864 0.8246.
could you please help me on this.
Sorry should have specified dimension for mean().
Also add in the max value for C, to generalise the loop.
If thre is an index not present in C then this will result in a row of NaNs.
for ii = 1:max(C)
B(ii,:) = mean(A(C == ii,:),1)
end
Andrei Bobrov
Andrei Bobrov am 14 Okt. 2019
Bearbeitet: Andrei Bobrov am 15 Okt. 2019
a = (1:max(C))';
b = accumarray(C,1);
i = a(accumarray(C,1) > 1);
[lo,ii] = ismember(C,i);
AA = A(lo,:);
[iii,j] = ndgrid(ii(lo),1:size(B,2));
B(i,:) = accumarray([iii(:),j(:)],AA(:),[],@mean);

4 Kommentare

I tired with the following code
A= [1.0119 1.6739
2.8012 1.6381
-0.0752 0.1222
1.3143 -0.8189
-0.7956 0.8750
1.8512 1.1499]
B =[0.9512 0.4314
0.2490 0.8309
0.3864 0.8246]
C =[ 3;
1;
2;
1;
2;
1];
[i,j] = ndgrid(C,1:size(A,2));
B = accumarray([i(:),j(:)],A(:),[],@mean)
when i run the code I couldnt able to get the result which i required.
I need to have the updated B should be B =[1.9889 0.6564;
-0.4354 0.4986;
0.3864 0.8246],the last row should be as existing B as 3 is present in only once in C.
Could you please help me on this.
I tried your code with respect to the following matrices
A=[1.0119 1.6739
2.8012 1.6381
0.0752 0.1222
1.3143 0.8189
0.7956 0.8750
1.8512 1.1499];
B=[0.3788 0.6150;
0.7567 0.4021;
0.5070 0.1980]
C =[1;
2;
3;
2;
2;
2]
a = unique(C)
b=accumarray(C,1)
i = a(accumarray(C,1) > 1)
lo = ismember(C,i)
AA = A(lo,:)
[ii,j] = ndgrid(C(lo),1:size(B,2))
B(i,:) = accumarray([ii(:),j(:)],AA(:),[],@mean)
When i run the code i am getting error stating Subscripted assignment dimension mismatch.
Error in line
B(i,:) = accumarray([ii(:),j(:)],AA(:),[],@mean)
Could you please help me on this.

Diese Frage ist geschlossen.

Gefragt:

am 14 Okt. 2019

Geschlossen:

am 20 Aug. 2021

Community Treasure Hunt

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

Start Hunting!

Translated by