是否可以在Matlab中将矩阵旋转45度 [英] Is it possible to rotate a matrix by 45 degrees in matlab

查看:845
本文介绍了是否可以在Matlab中将矩阵旋转45度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

即这样看起来就像钻石. (这是一个方形矩阵),每行比上一行增加1个元素,直到中间行的元素数量等于原始矩阵的尺寸,然后再向下移动,使每行回到1?

i.e. so that it appears like a diamond. (it's a square matrix) with each row having 1 more element than the row before up until the middle row which has the number of elements equal to the dimensions of the original matrix, and then back down again with each row back to 1?

推荐答案

旋转当然是不可能的,因为矩阵所基于的栅格"是规则的.

A rotation is of course not possible as the "grid" a matrix is based on is regular.

但是我记得您最初的想法是什么,因此以下内容将为您提供帮助:

But I remember what your initially idea was, so the following will help you:

%example data
A = magic(5);

A =

    17    24     1     8    15
    23     5     7    14    16
     4     6    13    20    22
    10    12    19    21     3
    11    18    25     2     9


d = length(A)-1;
diamond = zeros(2*d+1);

for jj = d:-2:-d

    ii = (d-jj)/2+1;
    kk = (d-abs(jj))/2;

    D{ii} = { [zeros( 1,kk ) A(ii,:) zeros( 1,kk ) ] };
    diamond = diamond + diag(D{ii}{1},jj);
end

将退还钻石:

diamond =

     0     0     0     0    17     0     0     0     0
     0     0     0    23     0    24     0     0     0
     0     0     4     0     5     0     1     0     0
     0    10     0     6     0     7     0     8     0
    11     0    12     0    13     0    14     0    15
     0    18     0    19     0    20     0    16     0
     0     0    25     0    21     0    22     0     0
     0     0     0     2     0     3     0     0     0
     0     0     0     0     9     0     0     0     0

现在,您可以再次逐行或逐列搜索单词或样式,只需删除零即可:

Now you can again search for words or patterns row by row or column by column, just remove the zeros then:

想象一下,您提取了一行:

Imagine you extract a single row:

row = diamond(5,:)

您可以使用find提取非零元素:

you can extract the non-zero elements with find:

rowNoZeros = row( find(row) )

rowNoZeros =

    11    12    13    14    15


不是真正的钻石,但也可能有用:


Not a real diamond, but probably useful as well:

(@ beaker评论中的想法.如果他自己发布,则将删除此部分.)

(Idea in the comments by @beaker. I will remove this part, if he is posting it by himself.)

B = spdiags(A)

B =

    11    10     4    23    17     0     0     0     0
     0    18    12     6     5    24     0     0     0
     0     0    25    19    13     7     1     0     0
     0     0     0     2    21    20    14     8     0
     0     0     0     0     9     3    22    16    15

这篇关于是否可以在Matlab中将矩阵旋转45度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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