what exactly norm(x) function do?
Show older comments
Good Afternoon,
I have to convert a part of my matlab code into C++.
I would be great if you can explain me what exactly norm(x) do on a vector of complex number so that I can write a C++ code for it.
I just asked a similar question on stack overflow but could not get much response.
Thank you in advance.
5 Comments
Manu Chaudhary
on 21 Sep 2022
Moved: Adam Danz
on 22 Sep 2022
Steven Lord
on 22 Sep 2022
Are you performing the conversion to C++ manually or using MATLAB Coder? According to the Extended Capabilities section on its documentation page you can automatically generate C or C++ code from the norm function using MATLAB Coder.
Manu Chaudhary
on 22 Sep 2022
Manu Chaudhary
on 22 Sep 2022
Edited: Manu Chaudhary
on 22 Sep 2022
Accepted Answer
More Answers (1)
Assuming you only care about p=2, If you already have code that computes norm(x) for real x, you can extend it to complex x via,
norm(x) = norm( [norm(real(x)), norm(imag(x)) ] )
which is easily verified below,
x=complex(rand(5,1), rand(5,1));
norm(x)
norm( [norm(real(x)), norm(imag(x)) ] )
Alternatively, if you have an implementation of abs, you could do norm(abs(x),p) which will work for any p-norm:
p=3;
norm(x,p)
norm(abs(x),p)
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!