For matrix A you cannot do: A.^2(:)
Show older comments
I have a matrix A=[1 2;3 4]. I want to sum the squares of the elements in the matrix. So I tried
sum(A.^2(:))
I cannot do this. The problem is A.^2(:). How do I solve this? Parenthesis around A.^2 does not help.
A workaround is
sum(reshape(A.^2,[],1))
Related to this is for example:
1+A(:)
This is OK. But this is not:
A+1(:)
Parenthesis around A+1 does not work.
2 Comments
A=[1 2;3 4];
1 + A(:)
% A + 1(:) not sure what this is expected to do?
% maybe
A + 1
% or
A(:) + 1
Eric
on 28 Jan 2024
Accepted Answer
More Answers (1)
A=[1 2;3 4];
sum(A(:).^2)
vecnorm(A(:))^2
norm(A,'fro')^2
Categories
Find more on Matrix Indexing 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!