Clear Filters
Clear Filters

Matrix reduction to see how many various elements the matrix has

2 views (last 30 days)
Dear All,
I have a 1*12 row whose elements are integers. Some of the elements might be repeated. I need to reduce the matrix to a smaller one which just contains the non-zero numbers disregarding of how many time they have been repeated.
for an Example
A = [4, 0, 0, 1, 4, 4, 2, 0, 1, 3, 9, 0]
The desired output is something like
Reduced = [ 4, 1, 2, 3, 9]
Please note that 0 should be excluded. Thank you so much for the help.

Accepted Answer

Stephen23
Stephen23 on 1 Feb 2016
Edited: Stephen23 on 1 Feb 2016
You could use unique with the 'stable' option:
>> A = [4, 0, 0, 1, 4, 4, 2, 0, 1, 3, 9, 0];
>> unique(A(A~=0),'stable')
ans =
4 1 2 3 9

More Answers (0)

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!