How can I fix this problem

2 ビュー (過去 30 日間)
fs
fs 2017 年 5 月 29 日
コメント済み: the cyclist 2017 年 5 月 30 日
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".

回答 (1 件)

the cyclist
the cyclist 2017 年 5 月 29 日
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 件のコメント
fs
fs 2017 年 5 月 29 日
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 2017 年 5 月 30 日
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));

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by