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

To resolve issues starting MATLAB on Mac OS X 10.10 (Yosemite) visit: http://www.mathworks.com/matlabcentral/answers/159016

I want to extract half of the non-zero element of a matrix

Asked by BANI tita on 14 Oct 2012

Hello, I have a matrix of the following form: [1,0,0,0;0,0,0,0;0,10,0,12;13,0,0,0], I need a mask that allows me to extract half of the data of each nonzero matrix of this form in this case for example I want the following result [1,0,0,0;0,0,0,0;0,10,0,0;0,0,0,0]

0 Comments

BANI tita

Products

No products are associated with this question.

2 Answers

Answer by Matt J on 14 Oct 2012
idx=find(A);
A(idx(2:2:end))=0;

1 Comment

BANI tita on 14 Oct 2012

merci

Matt J
Answer by Andrei Bobrov on 14 Oct 2012
ii = 1:fix(nnz(A)/2);
At = A';
jj = find(At);
At(jj(fix(nnz(A)/2)+1:end)) = 0;
out = At';

1 Comment

BANI tita on 14 Oct 2012

merci bien

Andrei Bobrov

Contact us