How can I fix this problem

2 vues (au cours des 30 derniers jours)
fs
fs le 29 Mai 2017
Commenté : the cyclist le 30 Mai 2017
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".

Réponses (1)

the cyclist
the cyclist le 29 Mai 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 commentaires
fs
fs le 29 Mai 2017
I used this build in function for normalization a=normc(A) and I got the right answer. However, I just started learning Matlab so I want to train myself for the for loop.
the cyclist
the cyclist le 30 Mai 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));

Connectez-vous pour commenter.

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by