divide each column of an array by its norm of the column

20 次查看(过去 30 天)
If there is array with size (2*3*128) and I want to divid each column over its norm.
How to do that?
For example this is part of the matrix
val(:,:,1) =
-0.0401 -0.2077 0.4750
-0.3867 -1.7742 3.3940
val(:,:,2) =
-0.0259 -0.1569 0.3964
-0.2504 -1.3830 3.1329
I want each column such as -0.0401 to divid it over its norm and same for other column
-0.3867
2nd column -0.2077 divid it over its norm
-1.7742
  6 个评论
Image Analyst
Image Analyst 2022-4-9
Arrays are indexed like (row, column, slice). So by column, you don't mean dimension 2
val = 5*rand(2,3,128);
col2 = val(:, 2, :); % Get the second column -- all values of dimension 2
whos col2
Name Size Bytes Class Attributes col2 2x1x128 2048 double
col2 = squeeze(col2) % Turn from 3-D array into 2-D matrix
col2 = 2×128
1.6494 1.7078 0.5240 4.1775 4.6723 0.8532 1.7875 2.8801 0.8354 4.9773 2.5038 4.2715 2.1833 2.8106 2.6810 0.2129 3.3235 1.1497 2.5837 4.5390 3.5507 3.4277 1.2060 3.0198 3.1786 1.5361 1.3881 4.4988 3.9208 2.5136 2.8813 2.0693 3.8127 1.7947 3.8008 4.7853 4.6437 3.5783 3.8805 4.8341 2.9445 0.0719 0.9767 1.8386 0.4462 2.3776 0.3446 2.2689 0.9295 1.4432 4.7759 4.8002 0.3142 1.1288 1.1407 2.2899 1.8586 0.8476 2.4115 2.9267
whos col2
Name Size Bytes Class Attributes col2 2x128 2048 double
but you mean like a vertical vector perpendicular to the slices going down through the 128 slices. Like one vector of 128 would be val(1,1,:), and another vector of 128 would be val(1,2,:), and so on. Is that right?

请先登录,再进行评论。

采纳的回答

Torsten
Torsten 2022-4-9
编辑:Torsten 2022-4-9
A = A./vecnorm(A,2)

更多回答(1 个)

Image Analyst
Image Analyst 2022-4-9
Is this possibly what you're trying to describe?
val = 5 * rand(2, 3, 128); % Sample data.
[rows, columns, slices] = size(val);
% Get norms
theNorms = zeros(rows, columns);
for col = 1 : columns
for row = 1 : rows
v = squeeze(val(row, col, :));
theNorms(row, col) = norm(v);
end
end
% Divide each value by the norm for that (row, column) location.
output = zeros(rows, columns, slices);
for slice = 1 : slices
for col = 1 : columns
for row = 1 : rows
thisNorm = theNorms(row, col);
output(row, col, slice) = val(row, col, slice) / thisNorm;
end
end
end

类别

Help CenterFile Exchange 中查找有关 Matrices and Arrays 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by