I have an array of all numeric values but it is a mix of integers and non-integers. How could I replace an non-integer with 0, for example? Also, the array is fairly large (about 130k rows).
Thanks in advance!

답변 (2개)

sixwwwwww
sixwwwwww 2013년 12월 6일
편집: sixwwwwww 2013년 12월 6일

0 개 추천

If 'a' is your array then use the following:
a(arrayfun(@(x) ~isinteger(x), a)) = 0;

댓글 수: 8

Muneer
Muneer 2013년 12월 6일
"isinteger", there's what I was looking for. Thanks so much.
iit works for integer type values only. if you have data in default MATLAB format(double type) then you should use the following:
a(arrayfun(@(x) mod(x, 1) ~= 0, a)) = 0
Because in case of default MATLAB format data it will make all the values 0 because they are not integer
Muneer
Muneer 2013년 12월 6일
The code you posted replaces the entire array with zeros for some reason. I just wanted the numbers that have a decimal place to be replaced with zero. Does matlab classify an non-integer as a non-number or something? ie. 0.5 and 3 are both integers?
sixwwwwww
sixwwwwww 2013년 12월 6일
편집: sixwwwwww 2013년 12월 6일
use this:
a(arrayfun(@(x) mod(x, 1) ~= 0, a)) = 0
I explained the reason above
Azzi Abdelmalek
Azzi Abdelmalek 2013년 12월 6일
Why arrayfun?
sixwwwwww
sixwwwwww 2013년 12월 6일
it could be done both ways but I am learning about arrayfun and cellfun as well so I used it. Since both solutions work well so I posted this one
Muneer
Muneer 2013년 12월 6일
Thanks, it works great!
sixwwwwww
sixwwwwww 2013년 12월 6일
you are welcome

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

카테고리

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

질문:

2013년 12월 6일

댓글:

2013년 12월 6일

Community Treasure Hunt

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

Start Hunting!

Translated by