필터 지우기
필터 지우기

arrayfun isreal fails - how to fix?

조회 수: 4 (최근 30일)
Alan
Alan 2019년 8월 7일
편집: Stephen23 2019년 8월 9일
x = randn(8,1);
z = fft(x);
assert(isreal(z(1)),'First');
y = arrayfun(@isreal,z);
assert(y(1),'Second');
In this snippet, I am trying to create a function which will return those elements in an array that are real.
The first assert passes, the first element of z is real.
The second assert fails as y thinks that every element in the array is not real.
Why?
How do I write a function that check whether each element in an array is real. Do I have to use a loop?
  댓글 수: 3
Alan
Alan 2019년 8월 7일
But z(1) is an array of size 1x1. Isn't everything an array in MATLAB?
And
isreal(z(1))
is true.
And the documentation for arrayfun says it applies the function func to the elements of A, one element at a time. So my code applies isreal to z(1) yet returns false.
Stephen23
Stephen23 2019년 8월 8일
편집: Stephen23 2019년 8월 9일
"Isn't everything an array in MATLAB?"
Yes. What you have uncovered is either inconsistent in arrayfun or the indexing, but either way is irrelevant to the question that you asked: "How do I write a function that check whether each element in an array is real." That is what I explained in my previous comment: being real or complex is a property of the entire array, so there is absolutely no point in testing each individual element. You seem to be confusing the values (i.e. having zero imaginary value) with an array property (real array vs. complex array).
Your question is like asking "how can I check if each element of a numeric array is numeric": of course each element of a numeric array is numeric. Ditto for real/complex arrays.
Test if the imaginary value is zero:
not(imag(z))

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

답변 (2개)

Guillaume
Guillaume 2019년 8월 7일
Note that the problem you highlight has nothing to do with isreal. I guess that you've uncovered an unexpected implementation detail of arrayfun. When dealing with complex numbers, it is not exactly equivalent to a loop.
First, some non-random testing data:
thetad = (0:90:360)';
z = cosd(thetad) + 1i*sind(thetad)
Simple test, let's extract each number into a cell array with a loop and with arrayfun:
>> carrayfun = arrayfun(@(x) x, z, 'UniformOutput', false)
carrayfun =
5×1 cell array
{[ 1 + 0i]}
{[ 0 + 1i]}
{[-1 + 0i]}
{[ 0 - 1i]}
{[ 1 + 0i]}
As you can see, each element is still complex, even those with imaginary part 0.
Now, with the equivalent loop:
>> cloop = cell(size(z)); for idx = 1:numel(z), cloop{idx} = z(idx); end; cloop
cloop =
5×1 cell array
{[ 1]}
{[0 + 1i]}
{[ -1]}
{[0 - 1i]}
{[ 1]}
The pure real elements are extracted as pure real. Note that num2cell does the same:
>> num2cell(z)
ans =
5×1 cell array
{[ 1]}
{[0 + 1i]}
{[ -1]}
{[0 - 1i]}
{[ 1]}
So, it must be an oddity of arrayfun, probably some optimisation detail. Probably worthy of a bug report to mathworks (if only so that the documentation explains what happens).
  댓글 수: 5
Bruno Luong
Bruno Luong 2019년 8월 8일
편집: Bruno Luong 2019년 8월 8일
Well the integer/complex field is a quite interesting math object, you can do operation like any better-known fields, but have some odd (but interesting) properties such as decomposition in prime "numbers" is not unique. I believe Gauss is the first one who are interested in such field.
People writing MEX interested in internal storage, but I think in case I don't care at all about how ARRAYFUN/CELLFUN behaves. I really use them, or when I use thme is by laziness.
I guess the odd effect we are discussing is due to TMW attempt to accelerate at all cost ARRAYFUN. I don't need them to document such special behavior, they are free to do whatevever they like.
James Tursa
James Tursa 2019년 8월 8일
편집: James Tursa 2019년 8월 8일
The problem with z == real(z) (and other methods posted here) is that it creates a temporary data copy of the real part of z. If you are working with large variables you don't really want that to happen. It would be nice if TMW included a function for this directly that didn't create this copy in the background. For now I guess we have to resort to mex routines to avoid the data copy.

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


Bruno Luong
Bruno Luong 2019년 8월 7일
편집: Bruno Luong 2019년 8월 7일
Guys ISREAL tests whereas the internal storage of an array does not allocated memory for the imaginary part, it is NOT testing the imaginary part is 0.
In recent MATLAB you can do this:
>> a = 1
a =
1
>> isreal(a)
ans =
logical
1
>> b=complex(a)
b =
1.0000 + 0.0000i
>> b==a
ans =
logical
1
>> isreal(b)
ans =
logical
0

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by