我如何使用 vertorisation 使函数规范化单元格数组的列,迭代每列的不同最大值?
How can I use vertorisation to have a function normalize columns of a cell array iterating through different max values for each column?
我正在尝试使用矢量化来让函数对每列的不同最大值的元胞数组的值进行归一化。
假设我有一个名为 holdArray
、
的元胞数组
holdArray = {1 2 3 4 5 6
7 8 9 10 11 12
13 14 15 16 17 18
19 20 21 22 23 24}
假设我有另一个元胞数组,其中包含 holdArray
的每一列的最大值,称为 maxVal
、
maxVal = {19 20 21 22 23 24}
我正在尝试使用 maxVal
中的值对 holdArray
中的列进行标准化,即 holdArray
中第一列中的每个值都将使用 holdArray
中的第一个值进行标准化=15=]等等。
我知道我可以使用 for 循环轻松地做到这一点,但是因为我将使用的数组非常大,所以我更愿意使用矢量化。
目前我是这样做的,其中 D 只是一个数组来保存标准化值 D = cellfun(@(x)(x./maxVal(1,:)), holdArray, 'UniformOutput', false)
哪个returns这个输出:
D =
[1x6 double] [1x6 double] [1x6 double] [1x6 double] [1x6 double] [1x6 double]
[1x6 double] [1x6 double] [1x6 double] [1x6 double] [1x6 double] [1x6 double]
[1x6 double] [1x6 double] [1x6 double] [1x6 double] [1x6 double] [1x6 double]
[1x6 double] [1x6 double] [1x6 double] [1x6 double] [1x6 double] [1x6 double]
意味着它对 maxVal
中所有值的列中的每个值进行标准化
任何关于如何使用矢量化来实现这一点的解释都将不胜感激。
谢谢
不确定为什么要使用 cell
作为双打?使用 cell2mat
将其转换为 matrix
,然后矢量化很容易...
holdArray = {1 2 3 4 5 6
7 8 9 10 11 12
13 14 15 16 17 18
19 20 21 22 23 24};
holdArray = cell2mat(holdArray); % Converts cell to matrix
maxVal = {19 20 21 22 23 24};
maxVal = cell2mat(maxVal);
% Make matrix same size as holdArray but entire column is maxVal
maxVal = repmat(maxVal, size(holdArray,1),1);
% Element-wise division to normalise
holdArray = holdArray./maxVal;
% ans =
%
% [0.0526 0.1000 0.1429 0.1818 0.2174 0.2500
% 0.3684 0.4000 0.4286 0.4545 0.4783 0.5000
% 0.6842 0.7000 0.7143 0.7273 0.7391 0.7500
% 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000]
% If you really want it back in a cell, you can use mat2cell
holdArray = mat2Cell(holdArray, size(holdArray,1));
因为 max
无论如何都对列进行操作,所以您根本不需要 cell/vector maxVal
,整个代码可以浓缩为:
holdArray = {1 2 ... }; % abbreviated, use above definition
holdArray = cell2mat(holdArray);
holdArray = holdArray ./ repmat(max(holdArray), size(holdArray,1), 1);
holdArray = mat2Cell(holdArray, size(holdArray,1));
我正在尝试使用矢量化来让函数对每列的不同最大值的元胞数组的值进行归一化。
假设我有一个名为 holdArray
、
holdArray = {1 2 3 4 5 6
7 8 9 10 11 12
13 14 15 16 17 18
19 20 21 22 23 24}
假设我有另一个元胞数组,其中包含 holdArray
的每一列的最大值,称为 maxVal
、
maxVal = {19 20 21 22 23 24}
我正在尝试使用 maxVal
中的值对 holdArray
中的列进行标准化,即 holdArray
中第一列中的每个值都将使用 holdArray
中的第一个值进行标准化=15=]等等。
我知道我可以使用 for 循环轻松地做到这一点,但是因为我将使用的数组非常大,所以我更愿意使用矢量化。
目前我是这样做的,其中 D 只是一个数组来保存标准化值 D = cellfun(@(x)(x./maxVal(1,:)), holdArray, 'UniformOutput', false)
哪个returns这个输出:
D =
[1x6 double] [1x6 double] [1x6 double] [1x6 double] [1x6 double] [1x6 double]
[1x6 double] [1x6 double] [1x6 double] [1x6 double] [1x6 double] [1x6 double]
[1x6 double] [1x6 double] [1x6 double] [1x6 double] [1x6 double] [1x6 double]
[1x6 double] [1x6 double] [1x6 double] [1x6 double] [1x6 double] [1x6 double]
意味着它对 maxVal
任何关于如何使用矢量化来实现这一点的解释都将不胜感激。
谢谢
不确定为什么要使用 cell
作为双打?使用 cell2mat
将其转换为 matrix
,然后矢量化很容易...
holdArray = {1 2 3 4 5 6
7 8 9 10 11 12
13 14 15 16 17 18
19 20 21 22 23 24};
holdArray = cell2mat(holdArray); % Converts cell to matrix
maxVal = {19 20 21 22 23 24};
maxVal = cell2mat(maxVal);
% Make matrix same size as holdArray but entire column is maxVal
maxVal = repmat(maxVal, size(holdArray,1),1);
% Element-wise division to normalise
holdArray = holdArray./maxVal;
% ans =
%
% [0.0526 0.1000 0.1429 0.1818 0.2174 0.2500
% 0.3684 0.4000 0.4286 0.4545 0.4783 0.5000
% 0.6842 0.7000 0.7143 0.7273 0.7391 0.7500
% 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000]
% If you really want it back in a cell, you can use mat2cell
holdArray = mat2Cell(holdArray, size(holdArray,1));
因为 max
无论如何都对列进行操作,所以您根本不需要 cell/vector maxVal
,整个代码可以浓缩为:
holdArray = {1 2 ... }; % abbreviated, use above definition
holdArray = cell2mat(holdArray);
holdArray = holdArray ./ repmat(max(holdArray), size(holdArray,1), 1);
holdArray = mat2Cell(holdArray, size(holdArray,1));