how to debug the non-working of real() function?

I have a 51x51 complex matrix and I only need its real value components. I am using real() to it but the operation is not being done? What to do? I have tried two methods but neither of them is working for me:
Method 1-
sinr_los1=real(sinr_los1);
Method 2-
for i=1:length(xAxis)
for j=1:length(yAxis)
sinr_los1(i,j)=real(sinr_los1(i,j));
end
end
Kindly suggest something

Answers (2)

Hi,
I guess sinr_los1 is your matrix ? Could you share the inputs and the outputs ?
Because both solutions should work
M =
1.0000 + 5.0000i 2.0000 + 3.0000i
4.0000 + 1.0000i -1.0000 - 8.0000i
>> real(M)
ans =
1 2
4 -1

9 Comments

I have tried this, as i said on the post
As said, the reason must be your matrix "sinr_los1". So we need it here or at least the output when you type
class(sinr_los1)
size(sinr_los1)
I can't share the file as it is confidential under research purposes.
Class is double and size is 51x51
And what do you get when you type
sinr_los1=real(sinr_los1);
?
The original complex matrix "sinr_los1" ?
Maybe - as John D'Errico said - you defined a function with name "real" ? Then you will have to rename it.
And what is the following code returning ?
real(sinr_los1)
At least can you explain what is it doing so we can help you
If i run this on the command window i get my desired values but if i only run the file in the standard manner, it seems that the system is skipping the line where I am taking the real part, ie line:
sinr_los1=real(sinr_los1);
Torsten, I get the original complex matrix, not the desired real part of it
That's impossible.
Well, i don't know what to say but cuss my luck

Sign in to comment.

When you get an error, don't just tell us that something is not working. Tell us the error message. The COMPLETE, error message, thus everything in red.
Would yo go to your doctor, and tell the doctor that something is not working, but not give a hint as to where you hurt?
Would you bring your car to a mechanic, and tell them only that you think the car has a problem, but nothing more?
BE CLEAR! You have information. MATLAB gives it to you, in the form of an error message, even if you don't understand the message you got. So tell us. We cannot see into your computer.
Without that information, since what you write is indeed valid, I can only conclude that you have defined a function or variable with the name real as the most likely possibility. Try this:
which real -all
Is there something you have defined with that name? If so, then when you try to use the real function, it will not work properly. You need to rename your own version, as MATLAB will be confused as to which real function (or variable) it should be using.

9 Comments

I am plotting sinr for an indoor wifi channel in 3D. I am using two variables which represent the x-axis and y-axis:
1) xAxis varies from 0 to 5 with step of 0.1 2) yAxis varies from 0 to 5 with step of 0.1 3) class of both variables is double 4) i am using surfc(x,y,z) to plot and its input values can't be negative
Since my x and y are the axes and there are sorted real values, issue is with Z which sinr_los1, it is a 51x51 matrix of class double and has complex values.
The command window is displaying:
hs=surf(cax,ages{:}); Error in surfc(line 54)
Referring to line where I write surfc(xAxis, yAxis, sinr_los1)
I have not named any of my variables with the function call as to ensure that the matlab system doesn't get confused
Therefore I used real function like the ways I wrote in my question post but facing issues
You know that you first have to use "meshgrid" on the arrays "xAxis" and "yAxis" before you can use them for "surfc" ?
[XAXIS,YAXIS] = meshgrid(xAxis,yAxis);
surfc(XAXIS,YAXIS,(sinr_los1+conj(sinr_los1))/2)
Yes, i am using the meshgrid for 3d plot
You wrote you use
surfc(xAxis, yAxis, sinr_los1)
with xAxis, yAxis being arrays of size 51. But they are matrices of size 51x51, aren't they ?
And what if you use the above code ?
(sinr_los1+conj(sinr_los1))/2
instead of
real(sinr_los1)
?
I just tried, it worked! Thanks a lot Torsten!
What did you change ?
Used conj trick instead of real, worked like a charm
Torsten
Torsten on 19 Oct 2022
Edited: Torsten on 19 Oct 2022
Then you should search for a function you created with name "real.m" and rename it. It's not usual that the MATLAB function "real" doesn't work like it should.
The issue is then apparently not with real, as you claimed. but with your use of surfc. So we are getting closer. (But you don't need meshgrid here.) And surfc won't care if the inputs are negative.
z = randn(51,51) + 1i*randn(51,51);
xAx = 0:.1:5;
yAx = 0:.1:5;
surfc(xAx,yAx,real(z))
As you can see, there is no problem in using real, with surfc, nor any need to use meshgrid. But you still need to give us enough information to diagnose the problem.

Sign in to comment.

Categories

Products

Asked:

on 19 Oct 2022

Edited:

on 19 Oct 2022

Community Treasure Hunt

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

Start Hunting!