remove column if value is greater than previous

11 views (last 30 days)
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 Comments
M
M on 12 Feb 2018
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" ?
Adam
Adam on 12 Feb 2018
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.

Sign in to comment.

Accepted Answer

Matt J
Matt J on 12 Feb 2018
Edited: Matt J on 12 Feb 2018
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 Comment
Paul vD
Paul vD on 13 Feb 2018
Thank you, This was very helpful. I wil try to break the code down to understand.

Sign in to comment.

More Answers (1)

Matt J
Matt J on 12 Feb 2018
Edited: Matt J on 12 Feb 2018
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 Comments
Stephen23
Stephen23 on 12 Feb 2018
mx3 = accumarray(subs,A(:,3),[],@max);
mx4 = accumarray(subs,A(:,4),[],@max);
[u,mx3,mx4].'
Paul vD
Paul vD on 12 Feb 2018
Edited: Paul vD on 12 Feb 2018
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?

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!