MATLAB:如何将矩阵的对角线作为另一个矩阵的参数传递? [英] MATLAB: how to pass in the diagonal of a matrix as an argument in another matrix?
本文介绍了MATLAB:如何将矩阵的对角线作为另一个矩阵的参数传递?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
让c
为2D数组,x
和y
为相同长度的1D数组(例如,让我们有x=1:7
和y=3:9
).
Let c
be 2D array, and x
and y
be 1D arrays of the same length (for instance, let's have x=1:7
and y=3:9
).
我需要找到一种方法,以下面将要描述的方式从x
和y
传递参数.
I need to find a way to pass in arguments from x
and y
in the way I will describe below.
如果我只是简单地按c(x,y)
,它将给出一个7 x 7的矩阵.我不要那个.
相反,我想传递[x y]
矩阵的对角线:((x(1), y(1)), (x(2), y(2))...(x(7), y(7))
.没有for循环或任何迭代语句,有没有办法做到这一点?
If I put simply c(x,y)
it will give a 7 by 7 matrix. I don't want that.
Instead, I want to pass in the diagonal of the [x y]
matrix: ((x(1), y(1)), (x(2), y(2))...(x(7), y(7))
. Is there a way to do this without a for loop or any iterative statement?
推荐答案
You are looking for sub2ind
function
res = c( sub2ind(size(c), x, y ) )
这篇关于MATLAB:如何将矩阵的对角线作为另一个矩阵的参数传递?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文