转化成矩阵沿对角线矢量 [英] transforming a matrix into a vector along its diagonals
问题描述
我不是不是一个程序员,我只需要解决一些数值在MATLAB。 我需要一个函数来进行以下改造为任何方阵:
im not not a programmer, i just need to solve something numerically in matlab. i need a function to make the following transformation for any square matrix:
从
row 1: 1 2 3
row 2: 4 5 6
row 3: 7 8 9
到
1 4 2 7 5 3 8 6 9
即写矩阵沿其对角线的向量自左右上角。 任何想法吗?
ie write the matrix in a vector along its diagonals from left to top right. any ideas please?
我真的需要多一点帮助,但:
i really need a little more help though:
说,我们已经转化为载体基质,具有记为M(I,J),其中i是行和j列的条目。现在我需要能够从在载体中的位置,在基质中的原来的位置,即找出说,如果在载体中其第三条目,我需要一个函数,会给我I = 1 J = 2。任何想法吗?我真的卡在这个:(谢谢
say the matrix that we have transformed into the vector, has entries denoted by M(i,j), where i are rows and j columns. now i need to be able to find out from a position in the vector, the original position in the matrix, i.e say if its 3rd entry in the vector, i need a function that would give me i=1 j=2. any ideas please? im really stuck on this:( thanks
推荐答案
这是相当类似于 previous 的问题上遍历矩阵Z字形顺序。随着轻微的修改,我们得到:
This is quite similar to a previous question on traversing the matrix in a zigzag order. With slight modification we get:
A = rand(3); %# input matrix
ind = reshape(1:numel(A), size(A)); %# indices of elements
ind = spdiags(fliplr(ind)); %# get the anti-diagonals
ind = ind(end:-1:1); %# reverse order
ind = ind(ind~=0); %# keep non-zero indices
B = A(ind); %# get elements in desired order
使用 SPDIAGS 功能。这样做的好处是,它任意矩阵尺寸(不只是正方形矩阵)对。例如:
using the SPDIAGS function. The advantage of this is that it works for any arbitrary matrix size (not just square matrices). Example:
A =
0.75127 0.69908 0.54722 0.25751
0.2551 0.8909 0.13862 0.84072
0.50596 0.95929 0.14929 0.25428
B =
Columns 1 through 6
0.75127 0.2551 0.69908 0.50596 0.8909 0.54722
Columns 7 through 12
0.95929 0.13862 0.25751 0.14929 0.84072 0.25428
这篇关于转化成矩阵沿对角线矢量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!