remove column if value is greater than previous
11 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Paul vD
el 12 de Feb. de 2018
Comentada: Paul vD
el 13 de Feb. de 2018
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 comentarios
M
el 12 de Feb. de 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
el 12 de Feb. de 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.
Respuesta aceptada
Matt J
el 12 de Feb. de 2018
Editada: Matt J
el 12 de Feb. de 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)].';
Más respuestas (1)
Matt J
el 12 de Feb. de 2018
Editada: Matt J
el 12 de Feb. de 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 comentarios
Stephen23
el 12 de Feb. de 2018
mx3 = accumarray(subs,A(:,3),[],@max);
mx4 = accumarray(subs,A(:,4),[],@max);
[u,mx3,mx4].'
Ver también
Categorías
Más información sobre Loops and Conditional Statements en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!