我正在使用单元格管理我正在处理的一些东西中的数据.我希望能够做到这样的事情:
A = cellfun( @(X)( randn( 5,5 ) ), cell( 5,1 ), 'UniformOutput', 0 );
B = cellfun( @(X)( randn( 5,5 ) ), cell( 5,1 ), 'UniformOutput', 0 );
%#
%# Each of the following would fail if cell member dimensions
%# don't match up
%#
%# matrix sums for each cell entry
%# C = A + B;
C = cellfun( @(X,Y)( X + Y ), A, B, 'UniformOutput', 0 );
%#
%# direct/hadamard product
%# D = A …Run Code Online (Sandbox Code Playgroud) 我正在编写一个MATLAB函数来将数据读出到一个n维数组(可变维度大小).我需要能够访问Matrix中的特定点(例如,写入或读取它),但我不知道要指定多少索引.
目前我有一个current_point向量迭代指定每个索引的max_points向量,以及一个指定数组大小的向量.所以,例如,如果我想要一个大小为1000×15×3的三维数组max_points = [1000 15 3],并且current_point迭代[1, 1, 1]到[1000, 15, 3]([1, 1, 1]- > [1000, 1, 1]- > [1, 2, 1]- > [1000, 2, 1]- > ...).我希望能够做的是current_point作为矩阵的索引,如下所示:
output_matrix(current_point) = val
Run Code Online (Sandbox Code Playgroud)
但显然output_matrix([1 2 3]) = val会有类似的东西outputmatrix(1:3) = 30.我不能只使用虚拟变量,因为有时矩阵需要3个索引,其他时间4,其他时间2等,所以我需要一个可变长度的矢量.是否有一种简单的方法可以将矢量用作索引中的点?
我有一个n-by-k大小的矩阵,每行包含k个数字.我想使用这些k数作为k维矩阵的索引.有没有在MATLAB中这样做的简洁方法,还是我必须使用for循环?
这是我想要做的(在MATLAB伪代码中),但是以更多的MATLAB方式:
for row=1:1:n
finalTable(row) = kDimensionalMatrix(indexmatrix(row, 1),...
indexmatrix(row, 2),...,indexmatrix(row, k))
end
Run Code Online (Sandbox Code Playgroud) 考虑矩阵M和存储在列I和J中的一组下标.我需要访问I&J指定的元素而不将它们转换为线性索引(即使用sub2ind).例如
M = [1 2 3;4 5 6;7 8 9];
I = [1 1 1];
J = [1 2 3];
VALS = [1 2 3];
Run Code Online (Sandbox Code Playgroud)
此外,由于I&J非常庞大,因此执行以下操作是不可行的:
VALS = diag(M(I,J));
Run Code Online (Sandbox Code Playgroud)
为了示范,这不是我想要的,
VALS = M(sub2ind(size(M),I,J));
Run Code Online (Sandbox Code Playgroud)
基本上sub2ind似乎花了很多时间,现在我正在寻找方法来访问这些元素而不将下标转换为索引.任何其他方式都是可行的,只要它比使用sub2ind的方法更快.
如何.^在MATLAB中重新定义指数函数?从:
x.^y
Run Code Online (Sandbox Code Playgroud)
至:
sign(x).*abs(x.^y))
Run Code Online (Sandbox Code Playgroud)