How to normalize rows of a matrix?

8 ビュー (過去 30 日間)
Kimberly Cardillo
Kimberly Cardillo 2020 年 8 月 19 日
回答済み: Steven Lord 2020 年 8 月 19 日
I have a matrix called aefm_1 that is 184 x 5119 where each row indicates a signal vector and each column is a time step and each value in the signal vector indicates amplitude at that time step. I want to write a loop that normalizes each signal vector in the matrix from -1 to 1 based on the maximum amplitude for each row. When I try to use the code below, it outputs only a singal vector of dimensions 1 x 5119. In the end, I want an output that is still 184 x 5119. How do I do that?
for i=1:size(aefm_1,1)
aefm_2 = aefm_1(i,:)./max(abs(aefm_1(i,:)));
end

採用された回答

Geoff Hayes
Geoff Hayes 2020 年 8 月 19 日
Kimberley - your code is overwriting the value that was assigned to aefm_2 on the previous iteration
aefm_2 = aefm_1(i,:)./max(abs(aefm_1(i,:)));
You will need to set this variable to be an array with the same dimensions as aefm_1 and then update each row of it. Alternatively, you could probably do this in a single line of code without a loop.
aefm_2 = aefm_1 ./ max(abs(aefm_1), [], 2);
See here for why we add the [] and 2 paramaters as inputs to max.

その他の回答 (1 件)

Steven Lord
Steven Lord 2020 年 8 月 19 日
Are you required to use a loop? If not, take a look at the normalize function.

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by