如何最好地构造一个矩阵,该矩阵的元素恰好是其索引或Matlab中索引的函数? [英] How to best construct a matrix whose elements are exactly their indices or functions of the indices in Matlab?

查看:77
本文介绍了如何最好地构造一个矩阵,该矩阵的元素恰好是其索引或Matlab中索引的函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

构造一个矩阵的最佳方法是什么,该矩阵的元素恰好是其在Matlab中的索引?

What is the best way to construct a matrix whose elements are exactly their indices in Matlab?

该问题的现有答案适用于如何构建矩阵为其元素为其索引的函数的矩阵.所以我将其添加到问题标题中.

The existing answers to this question is applicable to how to construct a matrix whose elements are functions of their indices. So I added that to the question title.

格式可以是将向量作为元素的矩阵,也可以是两个分别存储一个索引的矩阵.

The format can be either a matrix with vectors as elements, or as two matrices each storing one index.

最后,我想创建一个矩阵,其元素是其索引的函数.因此,非常感谢一种有效的方法(但可能有所不同).欢迎您对效率发表任何评论.

At the end, I would like to create a matrix whose elements are functions of their indices. So an efficient method for that (but possibly different) is very much appreciated. Any comment on efficiency is welcomed.

对于我的应用程序,矩阵的大小往往很大(最小尺寸为数百平方).结果,利用本机Matlab函数的方法可能比for/while循环要好得多.

The size of matrices tends to be large (dimension of hundreds squared at the minimum) for my applications. As a result, methods that take advantage of native Matlab functions are probably much better than for/while loops.

例如,对于大小为[2 2]的矩阵,我想选择其中一个

For example, for a matrix of size [2 2], I would like to make either

IND = 
[1 1]  [1 2]
[2 1]  [2 2]

X = 
1 1
2 2
Y =
1 2
1 2

最后,我希望做类似

matrixIneed = arrayfun(@(s)..., IND)

其中s是大小为2的向量,或者

where s is a vector of size 2, or

matrixIneed = arrayfun(@(i,j)..., X,Y)

首选后者.

关于已接受答案的注释.

A note about accepted answer.

我接受了安德鲁的回答,因为它对我来说很直观,而且代码看起来很快捷(至少对我而言).

I have accepted Andrew's answer because it is intuitive to me and the code seems quick (at least to me).

如果您曾经向Google提出过此问题的答案,那么您可能会像我一样担心性能. (否则,如果不是最佳实践,则任何人都可以想到双重循环来完成任务.)

If you ever Google an answer to this question, you are likely concerned about performance like I do. (Otherwise if not for best practice, anyone can think of a double loop to accomplish the task.)

如果是这样,鼓励您检查Andrew对reshape()函数的评论以及Rody关于meshgrid()loops性能的答案.

If so, you are encouraged to examine Andrew's comment on the reshape() function and Rody's answer about the performance of meshgrid() and loops.

尽管如此,使用meshgrid()的waywaywalk解决方案还是一个学习meshgrid()功能的有用示例.并且在许多其他Matlab函数中很有用.

Nevertheless, thewaywewalk's solution with meshgrid() is a helpful example to learn the meshgrid() function. And it is useful in many other Matlab functions.

跳汰机(jigg)的repmat()解决方案也可以为您提供帮助.

Jigg's repmat() solution may help you too.

推荐答案

看看ind2subsub2indreshape函数.它们对于转换多维数组上的下标和索引很有用.

Have a look at the ind2sub, sub2ind, and reshape functions. They're useful for transforming subscripts and indexes on multidimensional arrays.

在您的情况下,看起来像您想要的. (我认为您希望使用下标"而不是索引".当将数组视为一维矢量时,Matlab使用索引"表示元素的线性"索引,而使用下标"表示沿每个元素的位置多维数组的维度.)

In your case, it looks like you want this. (I think you want "subscripts" instead of "indices". Matlab uses "index" to mean the "linear" index of an element when viewing the array as a one-dimensional vector, and "subscripts" to mean the position along each dimension of a multidimensional array.)

sz = [3 3];  % Size of your matrix
n = prod(sz); % Total number of elements in matrix
[X, Y] = ind2sub(sz, 1:n);  % Subscripts, returned as vectors
X = reshape(X, sz);  % Reshape the subscript vectors to match your matrix
Y = reshape(Y, sz);

@thewaywewalk提供的meshgrid方法将产生相同的输出,但是我认为ind2sub方法在这种情况下更具可读性,因为它是根据数组索引(这是您的问题领域)来措辞的.而且它将泛化为对切片或数组的任意子集进行处理,而meshgrid则不会,并且与ind2sub很好地对应,以进行另一种有效的操作. (尽管还是值得学习meshgrid;它会弹出其他位置.)

The meshgrid approach that @thewaywewalk gave will produce the same output, but I think the ind2sub approach is a bit more readable in this case, since it's worded in terms of array indexing, which is your problem domain. And it will generalize to working on slices or arbitrary subsets of arrays, which meshgrid will not, and corresponds nicely to ind2sub for efficient operations going the other way. (It's worth learning meshgrid anyway though; it pops up in other locations.)

这篇关于如何最好地构造一个矩阵,该矩阵的元素恰好是其索引或Matlab中索引的函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆