square root of sum of squares on portions of cell arrays - Matlab Noob

I have a cell array, out5, with a single column of 2x1 vectors. I would like to take the square root of the sum of the squares of the two values in each vector for each of these vectors. I was looking at doing some code similar to this:
cellfun(@hypot,out5)
where,
out5 =
[0;0]
[0;-162]
[-54;72]
...
I would desire the output to be:
out6 = [sqrt(0^2 + 0^2) ; sqrt(0^2 + (-162)^2) ; sqrt((-54)^2 + 72^2) ; ... ]
I would also like the values to be either positive or negative depending on the first values in the vectors of out5. so for instance, the third vector in the cell array out5 [-54;72] the value output would be a negative since the first value is negative. In this example the final result would be:
[0 ; 162 ; -90]

3 Comments

first of all my friend square of any negative number will always be positive so you will always get the positive value of the defined negative value try defining it in terms of positive values (i.e positive ranges).
You're exactly right, but I want the value to be either positive or negative depending on whether or not the value of the first term is positive or negative.
what is your cell an image for example if you are using a image in grey scale the piccture image quality is defined on 100 for white and 0 for black what i mean to say the darkest of the darkest image will also be a positve one so you define your image in positive range

Sign in to comment.

 Accepted Answer

out5 = {[0;0],[0;-162],[-54;72]}
cellfun(@(vec)hypot(vec)*((-2*double(vec(1)<0))+1),out5)
or
cellfun(@(vec)sqrt(sum(vec.^2))*((-2*double(vec(1)<0))+1),out5)
ans =
0 162 -90
The bit at the end: ((-2*double(vec(1)<0))+1)
Will return 1 for a positive (or zero), and -1 for a negative. The sign() function almost does that, but it returns zero for zero, which isn't quite what you want.

4 Comments

I tried the following:
out6 = cellfun(@(vec)sqrt(sum(vec.^2))*sign(vec(1)),out5)
and got
out6 =
0
0
-90.0000
not [0 ; 162 ; -90]
I also tried
cellfun(@(vec)hypot(vec)*sign(vec(1)),out5)
and got
Error using hypot Not enough input arguments.
Hmmmmmm
Sorry... I posted too early and had a little (well, big) bug. It was fixed between when you saw my initial answer and your comment...
Ah yes, thanks so much for the help! I have to say this community is one of the most helpful on the web. It was the sign() function issue. I'll have to have a look at a video tutorial if I can find one on the cellfun() and arrayfun() operations as the matlab help function browser wasn't clear enough for me.
Mark, the best way to think of cellfun/arrayfun is this:
  1. Pretend you've got a variable (or variables) with names in the @(name1,name2) part.
  2. Write a 1-line command that you would do with that/those variables
So in my example I've got cellfun(@( vec )... This means I just have to write something that refers to some variable called vec, and it will actually be the contents of the cell I'm giving. It's basically equivalent to:
for i=1:numel(out5)
vec = out5{i};
% Now I need to do something with *vec*
end

Sign in to comment.

More Answers (0)

Asked:

on 28 Mar 2013

Community Treasure Hunt

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

Start Hunting!