Finding minimum value between certain rows
显示 更早的评论
Hi,
I have a 9x3 matrix.
A= [0.08 34.1 2; 0.03 34.2 2; 0.04 34.3 2; 0.05 34.4 1; 0.07 34.5 1; 0.04 34.6 1; 0.02 34.7 1; 0.03 34.8 2; 0.08 34.9 2]
I want to find the minimum value in the first column but not for all the rows together. As you can see the first three rows have a value of 2 in the third column so I want to find the minimum value in the first column for the first three rows. Then rows 4 to 7 all have a 1 in the last column so I want to find the minimum value in column 1 from rows 4-7. Then rows 8 to 9 have a 2 in the third column so I want to find the minimum value in the first column in rows 8-9. In the end I want this...
B= [0.03 34.2 2; 0.02 34.7 1; 0.03 34.8 2]
The second thing I want to do is similar to the first thing but instead of finding the minimum value I want to find the average of the column 1 from Matrix A between certain rows (same row selection as when finding the minimum). In the end I want this...
C= [0.05 2; 0.045 1; 0.055 2]
I hope that made sense. Thanks!
采纳的回答
更多回答(1 个)
Andrei Bobrov
2015-7-31
编辑:Andrei Bobrov
2015-7-31
i0 = [true;diff(A(:,3))~=0];
ii = cumsum(i0);
am = accumarray(ii,(1:numel(ii))',[],@(x){A(x(A(x,1)==min(A(x,1))),:)});
B = cat(1,am{:});
C = [accumarray(ii,A(:,1),[],@mean),A(i0,3)];
2 个评论
Azzi Abdelmalek
2015-7-31
编辑:Azzi Abdelmalek
2015-7-31
I will correct it like this
am= accumarray(ii,(1:numel(ii))',[],@(x) {A(find(A(x,1)==min(A(x,1)))+min(x)-1,:)})
Or
am= accumarray(ii,(1:numel(ii))',[],@(x) {A([ logical(zeros(min(x)-1,1));A(x,1)==min(A(x,1))],:)})
Andrei Bobrov
2015-7-31
Thank you Azzi. I am corrected.
am = accumarray(ii,(1:numel(ii))',[],@(x){A(x(A(x,1)==min(A(x,1))),:)});
类别
在 帮助中心 和 File Exchange 中查找有关 Mathematics 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!