Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

New to MATLAB?

how sort just nonzero values?

Asked by huda nawaf

huda nawaf (view profile)

on 12 Mar 2012

how can sort values except zero values i.e i want to sort just non zero values in c. how do that. c=[8 4 5 1 0 0 0 ];

thanks

0 Comments

huda nawaf

huda nawaf (view profile)

Tags

Products

No products are associated with this question.

2 Answers

Answer by Honglei Chen

Honglei Chen (view profile)

on 12 Mar 2012
Accepted answer

Do you want to keep all zeros at the original location? If so

c = [8 4 5 1 0 0 0];
c(c~=0) = sort(c(c~=0));

0 Comments

Honglei Chen

Honglei Chen (view profile)

Answer by Jonathan Sullivan

Jonathan Sullivan (view profile)

on 12 Mar 2012
c=[8 0 5 0 1 0 4];
c_new = sort(c,'descend')

1 Comment

huda nawaf

huda nawaf (view profile)

on 12 Mar 2012

thanks,
now if have matrix
c=[3 4 1 0; 2 1 0 0];
how sort nonzero values?

Jonathan Sullivan

Jonathan Sullivan (view profile)

Contact us