Calculating the norm of each row in a matrix [duplicate] - matlab

This question already has answers here:
applying norm function to rows of matrix - Matlab [duplicate]
(3 answers)
Closed 9 years ago.
I have a Nx3 matrix (A) the columns are X,Y,Z respectively. I want to calculate the norm that is sqrt(X^2+Y^2+Z^2) for each row. I did a for loop for that:
for i = 1:length(A)
Result(i) = norm(A(i,:))
end
is there any other way to do it avoiding for loop?
Thanks

You can do it like this:
sqrt(sum(A.^2, 2))
Your method returns a 1x3 where this returns a 3x1. So if you want you can transpose it but I doubt you really need to.

Related

How can I extract all the rows and create a 10x10 matrix from a 100x1 matrix Matlab [duplicate]

This question already has answers here:
Reshape column vector
(2 answers)
Closed last year.
r = randi(10,100,1);
I want to create a 10x10 matrix from the r matrix above
1st row will have the 10 first elements the 2nd row the next 10 and so on
Just use the reshape command. For instance,
M = reshape(r,10,10)';

Multiplication of each slice of a 3D matrix with 2D matrix [duplicate]

This question already has answers here:
Multiply a 3D matrix with a 2D matrix
(10 answers)
Closed 5 years ago.
I try to use svd in my work which will return the vector as below.
A=rand(10,5);
b=rand(10,5,100);
[U, S , V]=svd(A);
What I'm trying to do is to multiply each slice of b with U. With one specific slice, this operation is valid:
c=U'*b(:,:,1);
However when I try to use a vectorized method as below, it return array dimensions mismatch error.
Utran=U.';
c = cellfun(#(x) x.*b,num2cell(Utran,[1 2]),'UniformOutput',false);
I could probably use loop for the first method, but it's not efficient if I have a large matrix. Any idea what's my mistake here?
The following is a vectorized solution.
Not sure if it's faster than using loops; and even if it is, note that it uses more memorry for intermediate computations:
result = permute(sum(permute(conj(U), [1 3 4 2]).*b), [4 2 3 1]);

How to get Matrix from row-major vector in Matlab? [duplicate]

This question already has answers here:
Reshaping of Array in MATLAB
(3 answers)
Closed 7 years ago.
I have the following matrix:
50,60,55,67,70
62,65,70,70,81
72,66,77,80,69
I turn now the matrix into a vector but in row-major. This gives the following vector:
50,60,55,67,70,62,65,70,70,81,72,66,77,80,69
Now I would like to turn this vector into the same matrix as above. The problem is that reshape(matrix,[3,5]) does not work because Matlab operates column-major.
How can this be done efficiently (for large matrices)?
To solve this, use
reshape(matrix,[5,3]).'
First using reshape with row and column dimension swapped, you get a matrix with the right order but transposed, then using transpose you get the right output.
Having the control systems toolbox, you could also use vec2mat

applying norm function to rows of matrix - Matlab [duplicate]

This question already has answers here:
Vector norm of an array of vectors in MATLAB
(4 answers)
Closed 5 years ago.
I have a 3 columns, n rows matrix:
[ a,b,c;
d,e,f;
g,h,i; ]
I want to apply the norm function to each of the rows, and get a 1xn matrix containing the norms:
[ norm([a,b,c]);
norm([d,e,f]);
norm([g,h,i]); ]
I could do this with a for-loop, but is there a better way?
What about
norms = sqrt(sum(A.^2,1))
or
norms = sqrt(sum(A.^2,2))?
depending on whether your coordinates are in rows or in columns.
If readability is a bigger consideration than performance you might also consider:
norms = cellfun(#norm,num2cell(A,2));
This pattern is also adaptable to other operations along one dimension you might want to perform where MATLAB doesn't support it natively.
if the first dimension is not too large:
norms = sqrt(diag(A * A'));

Quick question about MATLAB elementwise division [duplicate]

This question already has an answer here:
Closed 11 years ago.
Possible Duplicate:
How do I divide the rows of a matrix by different values in MATLAB (array division)
I have a matrix A (size MxN) in Matlab and a Vector b with M rows and now I want to divide all elemtens in the i-th row of A by the i-th entry in b like a(i,:)/b(i) but I really don't want to use this sort since I than use a for-loop and I definitly need a FAST solution!
Could anybody help out? Thanks!
Edit: Somehow I just came up with it after posting... My solution is bsxfun(#rdivide, [1 1; 2 2; 3 3], [2 2 6]'), do you think that's a good and fast one?
You want to use bsxfunc :
bsxfun(#rdivide,A,B)
http://www.mathworks.com/help/techdoc/ref/bsxfun.html