MATLAB Answers

arrayfun isreal fails - how to fix?

6 ビュー (過去 30 日間)
Alan 2019 年 8 月 7 日
編集済み: Stephen Cobeldick 2019 年 8 月 9 日
x = randn(8,1);
z = fft(x);
y = arrayfun(@isreal,z);
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.
How do I write a function that check whether each element in an array is real. Do I have to use a loop?
  3 件のコメント
Stephen Cobeldick
Stephen Cobeldick 2019 年 8 月 8 日
"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:


回答 (2 件)

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 件のコメント
James Tursa
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 =
>> isreal(a)
ans =
>> b=complex(a)
b =
1.0000 + 0.0000i
>> b==a
ans =
>> isreal(b)
ans =


Community Treasure Hunt

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

Start Hunting!

Translated by