How can i delete max values for each 5 rows in vector

1 visualización (últimos 30 días)
i have this vector A= 1 14 4 23 3 8 9 12 4 5 2 4 19 20 22
A=A'
The result will be R= 1 14 4 3 8 9 4 5 2 4 19 20

Respuesta aceptada

Star Strider
Star Strider el 29 de Dic. de 2022
This looks like homework, however I need something to do this morning —
A = [1 14 4 23 3; 8 9 12 4 5; 2 4 19 20 22];
I = A<max(A,[],2); % Logical Matrix
for k = 1:size(A,1)
R(k,:) = A(k,I(k,:)); % Loop Necessary To Preserve Structure
end
R
R = 3×4
1 14 4 3 8 9 4 5 2 4 19 20
.
  2 comentarios
abdullah al-dulaimi
abdullah al-dulaimi el 29 de Dic. de 2022
vector please don't convert to matrix
Star Strider
Star Strider el 29 de Dic. de 2022
Now the transpose makes snese, with respect to ‘rows’.
Convert it to a matrix for the comparison, then back —
A= [1 14 4 23 3 8 9 12 4 5 2 4 19 20 22];
A = A.';
A = reshape(A,[],3);
for k = 1:size(A,2)
R(:,k) = A(A(:,k)<max(A(:,k)),k);
end
R = R(:).'
R = 1×12
1 14 4 3 8 9 4 5 2 4 19 20

Iniciar sesión para comentar.

Más respuestas (1)

Davide Masiello
Davide Masiello el 29 de Dic. de 2022
Editada: Davide Masiello el 29 de Dic. de 2022
The following code will apply to any lenght of A and any length N of window from which to delete the maximum, provided that length(A)/N is an integer.
A = [1 14 4 23 3 8 9 12 4 5 2 4 19 20 22];
R = rmvMaxEveryN(A,5)
R = 1×12
1 14 4 3 8 9 4 5 2 4 19 20
function out = rmvMaxEveryN(array,N)
[~,idx] = max(reshape(array,[N,length(array)/N]),[],1);
idx = idx+(0:N:length(array)-N);
array(idx) = [];
out = array;
end
  3 comentarios
abdullah al-dulaimi
abdullah al-dulaimi el 29 de Dic. de 2022
check the results above
Davide Masiello
Davide Masiello el 29 de Dic. de 2022
Sorry I realised later there was a mistake and adjusted my answer.

Iniciar sesión para comentar.

Categorías

Más información sobre Logical en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by