find indicies of maximum and minmum elements of an array in its each segmented smaller arrays, plus mean
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Limei Cheng
le 11 Août 2020
Modifié(e) : Bruno Luong
le 12 Août 2020
I have a large array A (1x3,000,000) and it was segmented by an indexA (1x5,000). Part of A (first 100 element) and indexA are as below:
A=[0 0 0 0 0 0 ...
0 0 0 0 0 0 ...
0 0 0 0 0 0 ...
0 0 0 0 0 0 ...
0 0 0 0 0 0 ...
0.4665982 1.0120536 1.0120536 1.0120536 1.0120536 1.0120536 ...
2.2720058 5.6612258 6.2438807 6.2438807 6.2438807 0.1706306 ...
3.4740953 7.5749402 8.3634338 8.3634338 8.3634338 0.9215111 ...
5.1563759 9.1346588 9.5029898 9.5029898 0.3987859 2.7512887 ...
7.7671609 11.3574429 11.4436398 0.3256813 1.5965981 5.1405444 ...
10.3805666 13.0157471 0 0.6612921 2.3093321 6.6952753 ...
11.4918060 12.5332108 0.1177574 1.0389878 3.9089594 8.5320177 ...
10.2545567 10.2545567 0.0723169 2.8528883 7.3057065 8.5047731 ...
0.0136646 0.8193274 4.6227546 8.9367990 9.6189337 0.2354149 ...
1.6503071 5.8358650 10.0257235 10.5574694 0.4393677 2.0512471 ...
6.2374287 10.5120144 11.1818972 0.4886205 2.0159214 5.8866639 ...
10.4216042 11.6498079 0.3699284 1.7069057];
indexA=[8 16 30 37 43 48 66 71 76 81 86 91 97];
The problem is to find index of max/min of A in each small segemented array based of the indexA, such as the segmented array are A(1:8-1), A(8:16-1),...A(91:97-1), A(97, end).
my solution using for loop is as below:
if indexA(1)>1
indexA=[1 indexA];
elseif indexA(end)<length(A)
indexA=[indexA length(A)];
end
Amax=[];idxAmax=[];Amin=[];idxAmin=[];Amean=[];
for i=1:length(indexA)-1
[val, idx] = max(A(indexA(i):indexA(i+1)-1);
idxAmax=[idxAmax idx+indexA(i)-1];
Amax =[Amax val];
[val, idx] = min(A(indexA(i):indexA(i+1)-1);
Amin =[Amin val];
idxAmin=[idxAmin idx+indexA(i)-1];
Amean=[Amean mean(A(indexA(i):indexA(i+1)-1)];
end
Is there other solution simpler and faster than using for loop? (The problem is updated. Elements of IndexA is the begining index of each segment in A now. And you can copy A directly to Matlab now).
0 commentaires
Réponse acceptée
Bruno Luong
le 11 Août 2020
Modifié(e) : Bruno Luong
le 11 Août 2020
Test example
A=1:100;
indexA=[8 16 30 37 43 48 66 71 76 81 86 91 97];
Code
I = zeros(size(A));
I([0 indexA]+1) = 1;
I(length(A)+1:end) = []; % incase indexA(end) contains the last index of A
I = cumsum(I);
[minAI,maxAI] = splitapply(@bounds, A, I);
meanAI = splitapply(@mean, A, I);
6 commentaires
Bruno Luong
le 12 Août 2020
Modifié(e) : Bruno Luong
le 12 Août 2020
Yeah as most of the recent MATHWORKS functions (SplitApply) it's slow. If speed is important and you are willing to go with histotical function, they usually faster. In this case the old ACCUMARRAY will do nicely the job and fast (which SPLITAPPLY tries to replace).
%%
A=1:1e6+1;
indexA=8:8:1e6;
tic
NewIndex=diff([0,indexA,numel(A)]);
cellA=mat2cell(A,1,NewIndex);
[~,idxMaxA]=cellfun(@max,cellA);
idxMaxA=idxMaxA+[0,indexA];
toc % Elapsed time is 0.346554 seconds.
tic
I = zeros(size(A));
I([0 indexA]+1) = 1;
I(length(A)+1:end) = []; % incase indexA(end) contains the last index of A
I = cumsum(I);
maxAI = splitapply(@max, A, I);
toc % Elapsed time is 2.071576 seconds.
tic
I = zeros(size(A));
I([0 indexA]+1) = 1;
I(length(A)+1:end) = []; % incase indexA(end) contains the last index of A
I = cumsum(I);
idxmaxAI = accumarray(I(:),A(:),[],@max)';
toc % Elapsed time is 0.120087 seconds.
Plus de réponses (1)
Fangjun Jiang
le 11 Août 2020
Modifié(e) : Fangjun Jiang
le 11 Août 2020
Here is one way to find the max index without for-loop. Not sure if it is faster for larger data
%%
A=1:100;
indexA=[8 16 30 37 43 48 66 71 76 81 86 91 97];
NewIndex=diff([0,indexA,numel(A)]);
cellA=mat2cell(A,1,NewIndex);
[~,idxMaxA]=cellfun(@max,cellA);
idxMaxA=idxMaxA+[0,indexA]
idxMaxA =
8 16 30 37 43 48 66 71 76 81 86 91 97 100
5 commentaires
Fangjun Jiang
le 12 Août 2020
Answer for the modified question.
NewIndex=diff([1,indexA,numel(A)+1]);
cellA=mat2cell(A,1,NewIndex);
[~,idxMaxA]=cellfun(@max,cellA);
idxMaxA=idxMaxA+[0,indexA-1]
idxMaxA =
7 15 29 36 42 47 65 70 75 80 85 90 96 100
Voir également
Catégories
En savoir plus sur Matrix Indexing dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!