delete some matrix elements
Afficher commentaires plus anciens
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 commentaires
Image Analyst
le 1 Oct 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
le 1 Oct 2014
Réponse acceptée
Plus de réponses (1)
Jeremy Kemmerer
le 1 Oct 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 commentaire
Patrick
le 1 Oct 2014
Catégories
En savoir plus sur Resizing and Reshaping Matrices dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!