具有行和列索引的矩阵上的八度映射
Octave map over matrix with row and column indexes
所以基本上我在寻找类似 arrayfun(@(value, rowIdx, colIdx), matrix)
的东西。
我需要根据矩阵的值及其索引从另一个矩阵创建一个矩阵,有没有办法避免 for 循环?
您可以使用 meshgrid
和矩阵的大小为行和列索引创建矩阵。然后,您可以使用所有这三个矩阵来计算结果。
[col_index, row_index] = meshgrid(1:size(matrix, 1), 1:size(matrix, 2));
% Now do some calculations using that
new_matrix = matrix + row_index * col_index;
所以基本上我在寻找类似 arrayfun(@(value, rowIdx, colIdx), matrix)
的东西。
我需要根据矩阵的值及其索引从另一个矩阵创建一个矩阵,有没有办法避免 for 循环?
您可以使用 meshgrid
和矩阵的大小为行和列索引创建矩阵。然后,您可以使用所有这三个矩阵来计算结果。
[col_index, row_index] = meshgrid(1:size(matrix, 1), 1:size(matrix, 2));
% Now do some calculations using that
new_matrix = matrix + row_index * col_index;