remove column if value is greater than previous

Hi, I am quite new to matlab and i am in need of some help.
Lets say I have a matrix: [2 2 2 3 3 3 3 3 3 3 5; 6 6 7 2 2 2 2 2 2 2 6; 2 1 3 1 2 3 2 4 1 6 1]
My goal is to shrink the matrix to: [2 2 3 5; 6 7 2 6; 2 3 6 1]
I am looking for a code where I want to keep the maximum values for row three, where row1 and row2 have the same values.
My code should look something like this:
for count:1:end
if matrix(1,count)==matrix(1,count-1) && matrix(1,count)==matrix(1,count-1) && matrix(3,count-1)<matrix(3,count)
then remove previous column.
Code isn't finished, but this was just to give an rough idea.
The problem is that the for loop doesn't seem to work when i remove a column (because the column counter continues counting), and also when it removes the previous column there is no reference to compare to anymore.
The matrix above is an example, I thought about a for-loop, because the actual matrix has over 20000 columns.
Could somebody give me a some help me with the code?
Thanks in advance, Paul

댓글 수: 2

M
M 2018년 2월 12일
It does not make a big difference, but the example you give is a vector, are you working with matrix or vector ?
The only problem is that the for loop doesn't seem to work
Could you define more precisely "does not seem to work" ?
I don't understand the logic of what you are trying to remove here.
Do you mean your matrix is:
[2 2 2 3 3 3 3 3 3 3 5; 6 6 7 2 2 2 2 2 2 2 6; 2 1 3 1 2 3 2 4 1 6 1]
? The one you posted is just a single row matrix of 33 columns.
Given this input though I can't see how you end up with [2 2 3 5; 6 7 2 6; 2 3 6 1] based on your logic. e.g. you have a column of [3; 2; 4] that you have got rid of in your answer.

댓글을 달려면 로그인하십시오.

 채택된 답변

Matt J
Matt J 2018년 2월 12일
편집: Matt J 2018년 2월 12일

0 개 추천

For the 5-row case, this might be what you want:
load A
x=A(1:2,:).';
y=A(3:5,:).';
M=size(x,1);
[u,~,subs]=unique(x,'rows','stable');
ycell=accumarray(subs,(1:M).',[],@(k) {y(k,:)});
for i=1:numel(ycell)
[~,j]=max(ycell{i}(:,3));
ycell{i}=ycell{i}(j,:);
end
result=[u, cell2mat(ycell)].';

댓글 수: 1

Paul vD
Paul vD 2018년 2월 13일
Thank you, This was very helpful. I wil try to break the code down to understand.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Matt J
Matt J 2018년 2월 12일
편집: Matt J 2018년 2월 12일

2 개 추천

A=[2 2 2 3 3 3 3 3 3 3 5; 6 6 7 2 2 2 2 2 2 2 6; 2 1 3 1 2 3 2 4 1 6 1].';
[u,~,subs]=unique(A(:,1:2),'rows','stable');
umax=accumarray(subs,A(:,3),[],@max);
result=[u,umax].'

댓글 수: 5

Stephen23
Stephen23 2018년 2월 12일
편집: Stephen23 2018년 2월 12일
+1 very tidy, a nice use of accumarray.
Paul vD
Paul vD 2018년 2월 12일
편집: Paul vD 2018년 2월 12일
Wauw, this answer is a bit shorter than i expected. Could you break this code down for me and explain a bit what happens?
(sorry about my last comment, made an error) It works perfectly. Thank you!!
Paul vD
Paul vD 2018년 2월 12일
My actual matrix is a bit larger (more rows). I want to keep the 3rd and 4th row for these maxima. Do you have any advice to do that?
I added my matrix
mx3 = accumarray(subs,A(:,3),[],@max);
mx4 = accumarray(subs,A(:,4),[],@max);
[u,mx3,mx4].'
Paul vD
Paul vD 2018년 2월 12일
편집: Paul vD 2018년 2월 12일
Thanks Stephen,
It is not the max what I am looking for, but the actual value of row 3 and row 4, for the maximum of row 5 (per combination of row 1 and row2). It seems that your solution only gives the maxima for row3 and row 4 for each combination of row1 and row2.
Is that correct?
With earlier given data set i would like to end up with the following vector for row1=4, row2=7
[4;7;6.5;1;81427]
I have now:
[u,~,subs]=unique(A(:,1:2),'rows','stable');
umax=accumarray(subs,A(:,5),[],@max);
mx3 = accumarray(subs,A(:,3),[],@max );
mx4 = accumarray(subs,A(:,4),[],@max );
result=[u,umax].';
result1=[u,mx3,mx4].';
where the unwanted answer is:
[4;7;10;10;81427]
Do you maybe have a solution?

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

질문:

2018년 2월 12일

댓글:

2018년 2월 13일

Community Treasure Hunt

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

Start Hunting!

Translated by