Remove zeros and perform calculation with non-zero elements

11 ビュー (過去 30 日間)
Damith
Damith 2014 年 6 月 2 日
コメント済み: Damith 2014 年 6 月 2 日
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 件のコメント
Damith
Damith 2014 年 6 月 2 日
How can I select the non-zero elements of B to a different array?
per isakson
per isakson 2014 年 6 月 2 日
See Seans answer.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by