我可以将 MATLAB 切片存储在变量中吗?
我有一个很长的切片序列,需要将其应用于许多 MATLAB 矩阵。我该怎么做?
即我可以简化
y(1:some_var*3,1:some_other_var*3,1:another_var*3) = x1(1:some_var*3,1:some_other_var*3,1:another_var*3) .* x2(1:some_var*3,1:some_other_var*3,1:another_var*3) ./ x3(1:some_var*3,1:some_other_var*3,1:another_var*3)
为类似的东西,
inds = slice(1:some_var*3,1:some_other_var*3,1:another_var*3)
y(inds) = x1(inds) .* x2(inds) ./ x3(inds)
就像我可以在Python中做的那样吗?
I have a lengthy slice sequence that I need to apply to lots of MATLAB matrices. How can I do this?
i.e. can I simplify,
y(1:some_var*3,1:some_other_var*3,1:another_var*3) = x1(1:some_var*3,1:some_other_var*3,1:another_var*3) .* x2(1:some_var*3,1:some_other_var*3,1:another_var*3) ./ x3(1:some_var*3,1:some_other_var*3,1:another_var*3)
to something like,
inds = slice(1:some_var*3,1:some_other_var*3,1:another_var*3)
y(inds) = x1(inds) .* x2(inds) ./ x3(inds)
like I can do in Python?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在您的情况下,您可以创建一个逻辑掩码:
您可能想阅读的其他函数: 查找,SUB2IND
In your case, you can create a logical mask:
Other functions that you might want to read about: FIND, SUB2IND
一种选择是将每个索引向量存储在 元胞数组的元胞中,然后将元胞数组内容提取为 逗号分隔列表如下所示:
如果您有大型矩阵,且索引集相对较小/稀疏,则此可能比使用Amro 建议的逻辑掩码。
One option is to store each vector of indices in a cell of a cell array, then extract the cell array content as a comma-separated list like so:
If you have large matrices, with relatively small/sparse sets of indices, this may be more efficient than using a logical mask as Amro suggested.