# Save mean values in additional column

1 view (last 30 days)
Daeyeon Koh on 27 Nov 2021
Answered: DGM on 27 Nov 2021
j=1;
for i=1:rowC
if Allpeaks(i,2)>4.33&&Allpeaks(i,2)<5.13
Range1peaks(j,:)=Allpeaks(i,:);
j=j+1;
end
end
[aaaaa_DummyIdx,Peaks_index1]=sort(Range1peaks(:,3));
Arg_Range1peaks=Range1peaks(Peaks_index1(1:1:end,:),:);
Sputum_Idx(:,1) = unique(Arg_Range1peaks(:,3));
for i = 1:numel(Sputum_Idx(:,1))
ind=(Arg_Range1peaks(:,3)==Sputum_Idx(i,1));
MeanV1(i,1) = mean(Arg_Range1peaks(ind));
end
Mean1 = [MeanV1 Sputum_Idx(:,1)]; Hello. After obtaining the N x 3 vector as shown in the picture above ([Range1 peaks]), I listed it in order of size in columns 3 through indexing ([Arg_Range1 peaks]).
In addition, the average values of column 1 values([Arg_Range1 peaks]) were saved for rows with the same values in column 3 ([Mean1]).
As shown in the rightmost figure, we would like to place mean values of [Mean1] corresponding to column 3 in column 4 of [Arg_Range1 peaks]. What should I do?

DGM on 27 Nov 2021
Something like this
% dummy data
N = 15;
Range1peaks = [10*rand(N,1)-70 2*rand(N,1)+4 randi([0 5],N,1)]
Range1peaks = 15×3
-64.2527 5.7079 1.0000 -61.6632 5.7425 3.0000 -67.5955 5.6379 4.0000 -60.7405 5.1714 1.0000 -66.4285 5.7260 3.0000 -60.1289 5.2518 2.0000 -64.3149 5.8483 4.0000 -69.3145 4.4015 4.0000 -66.4578 4.5760 5.0000 -69.5323 4.9063 3.0000
% sort
[~,Peaks_index1] = sort(Range1peaks(:,3),1,'ascend');
Arg_Range1peaks = Range1peaks(Peaks_index1,:);
% append column, take mean, fill
Arg_Range1peaks = [Arg_Range1peaks zeros(size(Arg_Range1peaks,1),1)];
Sputum_Idx = unique(Arg_Range1peaks(:,3));
for i = 1:numel(Sputum_Idx)
ind=(Arg_Range1peaks(:,3)==Sputum_Idx(i));
Arg_Range1peaks(ind,4) = mean(Arg_Range1peaks(ind,1));
end
% show result
Arg_Range1peaks
Arg_Range1peaks = 15×4
-65.2151 5.6245 0 -66.5816 -67.9481 4.8178 0 -66.5816 -64.2527 5.7079 1.0000 -64.6457 -60.7405 5.1714 1.0000 -64.6457 -65.2107 5.4760 1.0000 -64.6457 -68.3787 4.5315 1.0000 -64.6457 -60.1289 5.2518 2.0000 -60.1289 -61.6632 5.7425 3.0000 -65.8747 -66.4285 5.7260 3.0000 -65.8747 -69.5323 4.9063 3.0000 -65.8747

R2019b

### Community Treasure Hunt

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

Start Hunting!