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

查看:24
本文介绍了如何最好地构造一个矩阵,其元素正是它们的索引或 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.

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

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.

尽管如此,thewaywewalk 使用meshgrid() 的解决方案是学习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天全站免登陆