delete some matrix elements

298 visualizzazioni (ultimi 30 giorni)
Patrick
Patrick il 1 Ott 2014
Commentato: Patrick il 1 Ott 2014
I want to delete the elements that are larger than a certain value, then the next element will replace the deleted one(shift forward). For example,
[1 2 9;
3 1 4;
2 9 7]
, delete those larger than 4, the output will be
[1 2 3;
1 4 2]
If the no. of elements is not the multiple of 3, the remaining ones will be deleted. Could anyone give me some ideas? Thanks everyone!
  2 Commenti
Image Analyst
Image Analyst il 1 Ott 2014
Matrices have to remain rectangular. What would you want the answer to be if you deleted elements greater than 8 so just the 2 9's get removed? You can't have a matrix with 3 elements in the first row and second row and just one element in the last row. I.e. you can't have
[1 2 3
1 4 2
7 ]
Patrick
Patrick il 1 Ott 2014
Sorry, the remaining ones will be deleted. How to do that? Thanks dude!

Accedi per commentare.

Risposta accettata

Guillaume
Guillaume il 1 Ott 2014
The way to delete elements of an array that fulfill a given condition such as greater than is with:
A(A > value) = [];
So in your case:
A(A > 4) = [];
Note that with a matrix, it will flatten it into a column vector, as there's no guarantee it deletes enough element to keep the same number of columns (or rows, or whatever dimension you want to stay the same).
For example, what matrix would you want if you started with
A = [1 2 9; 3 1 4; 2 2 7];
If you know that the number of elements deleted is always going to produce a valid rectangular matrix with the same number of columns as you started with, the following would work:
ncol = size(A, 2);
A(A > 4) = [];
A = reshape(A, [], ncol); %will error if the number of elements remaining in A is not a multiple of ncol
  3 Commenti
Guillaume
Guillaume il 1 Ott 2014
It's the same syntax, but you give the index of the elements you want to delete directly. In that case it's the remainder (or modulo) of the number of elements after your deletion divided by the number of columns, counted from the end, thus:
ncol = size(A, 2);
A(A > 4) = []; %remove all elements greater than 4
todelete = mod(numel(A), ncol); %surplus to delete
A(end-todelete:end) = [];
A = reshape(A, [], ncol); %now guaranteed to succeed
Patrick
Patrick il 1 Ott 2014
Thank you so much!

Accedi per commentare.

Più risposte (1)

Jeremy Kemmerer
Jeremy Kemmerer il 1 Ott 2014
Hi Patrick,
A straightforward way to find and alter matrix elements with a certain property is logical indexing. In your case, if we call your matrix A, you could try something like:
>>idx = A>4; //create logical array
>>A(idx) = []; //remove array elements greater than 4
This will produce a row vector of the remaining elements, which you can reform into a matrix using the “reshape” command.
Please refer to the following documentation for more information on the “reshape” function: http://www.mathworks.com/help/matlab/ref/reshape.html
Also, there is a blog topic on MATLAB Central which provides more information on logical indexing: http://blogs.mathworks.com/loren/2013/02/20/logical-indexing-multiple-conditions/
  1 Commento
Patrick
Patrick il 1 Ott 2014
Thank you Jeremy for your answer and the relevant materials!

Accedi per commentare.

Community Treasure Hunt

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

Start Hunting!

Translated by