divide each column of an array by its norm of the column
5 views (last 30 days)
Show older comments
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 Comments
Image Analyst
on 9 Apr 2022
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
col2 = squeeze(col2) % Turn from 3-D array into 2-D matrix
whos col2
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?
More Answers (1)
Image Analyst
on 9 Apr 2022
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
See Also
Categories
Find more on Matrices and Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!