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
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" ?
Adam
2018년 2월 12일
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.
채택된 답변
추가 답변 (1개)
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].'
카테고리
도움말 센터 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!