Remove zeros and perform calculation with non-zero elements

Hi,
I have a array of B with 403 elements (403 x 1). Some of the values are qual to zero but I want to remove the zeros from the array and perform a calculation with non-zero elements. After the calculation with non-zero elements, the new values need to be replaced with old values.
Can anybody help me how I can do it MATLAB?
Thanks,
Dumindu.

답변 (2개)

Sean de Wolski
Sean de Wolski 2014년 6월 2일
편집: Sean de Wolski 2014년 6월 2일
Extract the elements in C where B is nonzero
D = C(B~=0)
Extract the nonzero elements of B into D
D = nonzeros(B)
per isakson
per isakson 2014년 6월 2일
편집: per isakson 2014년 6월 2일
Hint
B(not(B==0)) = perform_calculation( B(not(B==0)) );
where
function M2 = perform_calculation( M1 )
% calculations
end
If calculations use more variables make perform_calculation a nested function to make the variables of the caller available to calculations.

댓글 수: 2

How can I select the non-zero elements of B to a different array?
See Seans answer.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

질문:

2014년 6월 2일

댓글:

2014년 6월 2일

Community Treasure Hunt

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

Start Hunting!

Translated by