# Make a matrix smaller based on some criteria

5 visualizaciones (últimos 30 días)
Md el 2 de En. de 2014
Editada: Image Analyst el 2 de En. de 2014
I am working with a matrix.
A=[...
20 40 21.5 41
5 21 15 10]
Now, first of all, I want to find out the similar values from row 1. Lets say, the similar values are 20 and 21.5, another pair is 40 and 41. For the each pair, I want to eliminate one column from each pair based on their corresponding column value in the second row. So, I want to eliminate
41
10
and
20
5
because its value in the second row is smaller. The final matrix that I want is
21.5 40
15 21
##### 0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

Md el 2 de En. de 2014
row 1 is 20 40 21.5 41 and second row is 5 21 15 10
##### 1 comentarioMostrar -1 comentarios más antiguosOcultar -1 comentarios más antiguos
Image Analyst el 2 de En. de 2014

Iniciar sesión para comentar.

### Más respuestas (1)

Image Analyst el 2 de En. de 2014
You'll need to specify how close is close enough to be removed. Is it 1 away? 0.5 away? 2 away? Then I'd start scanning columns, and if any column is too close to another one, delete it:
% Delete column 42:
A(:,42) = [];
##### 3 comentariosMostrar 1 comentario más antiguoOcultar 1 comentario más antiguo
Image Analyst el 2 de En. de 2014
OK, I think the algorithm should work. Just make sure you use a while instead of a for because the array will be shrinking as your scan along and you need to take that into account. Post your code if you have trouble.
Image Analyst el 2 de En. de 2014
Editada: Image Analyst el 2 de En. de 2014
See if this works for you:
rows = 2;
columns = 40;
A = randi(99, rows, columns) % 2 by 40 matrix of values from 1-99.
tooClose = 2;
column = 1;
lastColumn = columns;
while column < lastColumn
% Find difference between current column and all the other columns.
differences = abs(A(1,:) - A(1, column));
% Find out which ones are too close and need to be deleted.
columnsToDelete = find(differences <= tooClose);
if length(columnsToDelete) > 1
% Don't delete the last one.
columnsToDelete = columnsToDelete(1:end-1);
% Now delete those columns
A(:, columnsToDelete) = [];
% Get new size.
[rows, lastColumn] = size(A);
end
% Increment column we're looking at
column = column + 1;
end
% Report final version of A to the command window.
A

Iniciar sesión para comentar.

### Categorías

Más información sobre Matrices and Arrays 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!

Translated by