How can I fix this problem
2 views (last 30 days)
Show older comments
I am trying to get a normalized matrix.
A=[1 1 2 4 7 1;0 2 2 1 3 9;
1 2 1 1 1 9;1 3 0 1 4 1];
R=size (A,1); % Row
C=size (A,2); % Column
for i=1:C
N=A(:,i)/sqrt(sum(A(:,i).^2));
S(i,:)=N
end
I do not get a right rows, then I tried a transpose and it does not work "nothing change".
0 Comments
Answers (1)
the cyclist
on 29 May 2017
When you say "normalized", what specifically do you mean?
The way you did it, your result is such that
sum(S.^2,2)
is a column vector of 1's, which is a form of normalization.
3 Comments
the cyclist
on 30 May 2017
Well, the rows of S have the correct normalization. You are overwriting N during each iteration of the for loop. So, this would have worked:
A=[1 1 2 4 7 1;0 2 2 1 3 9;
1 2 1 1 1 9;1 3 0 1 4 1];
R=size (A,1); % Row
C=size (A,2); % Column
for i=1:C
N(:,i)=A(:,i)/sqrt(sum(A(:,i).^2));
S(i,:)=N(:,i)
end
You can also avoid the for loop completely:
A=[1 1 2 4 7 1;0 2 2 1 3 9;
1 2 1 1 1 9;1 3 0 1 4 1];
N = A./sqrt(sum(A.^2));
See Also
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!