How to find magnitude of an array of 17 vectors

8 ビュー (過去 30 日間)
Sarah Hicks
Sarah Hicks 2018 年 9 月 3 日
回答済み: Steven Lord 2019 年 6 月 24 日
I have imported a txt file and need to find the magnitude of the 17 vectors on the file. Thanks.

採用された回答

madhan ravi
madhan ravi 2018 年 9 月 3 日
編集済み: madhan ravi 2018 年 9 月 3 日
Use :
norm(x) %x - is a vector, norm syntax calculates the magnitude of the vector
*AN EXAMPLE: *
X= rand(10,10)
size(X)
for i = 1:length(X)
MAG(i,:) = norm(X(i,:)); %calculates the magnitude of 10 vectors separately in each iteration
end
MAG
size(MAG)
  9 件のコメント
Image Analyst
Image Analyst 2018 年 9 月 3 日
Length() gives the longest dimension. I don't think it's a good idea to use it on a matrix. I use it only on vectors. For some reason the lamelli.txt file got removed from the post. If it's only 2 or 3 columns, it should be okay but if it has more than 17 columns, there will be a problem. Plus you shouldn't use i as a loop index, and you don't need a second dimension for MAG. To be more robust, here is how I'd do it:
data = load('lamelli.txt')
[rows, columns] = size(data)
magnitudes = zeros(rows, 1); % Preallocate
for k = 1: rows
magnitudes(k) = norm(data(k,:));
end
magnitudes % Print to command window.
whos magnitudes % Print info to command window.
madhan ravi
madhan ravi 2018 年 9 月 3 日
Thank you @sir Image Analyst much appreciated.

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

その他の回答 (2 件)

Steven Lord
Steven Lord 2019 年 6 月 24 日
If you're using release R2017b or later, use the vecnorm function to compute the norm of vectors stored in an array.

Joshua Daniels
Joshua Daniels 2019 年 6 月 24 日
編集済み: Joshua Daniels 2019 年 6 月 24 日
sqrt(sum(data.^2,dim)) where dim is the dimention that your vectors are written in. ie a 5x3 vertical list of horizontal vectors would use dim=2 since each vector is in the horizontal dimention.

カテゴリ

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