在Matlab中将strfind用于矩阵中的不同对角线方向

在Matlab中将strfind用于矩阵中的不同对角线方向

本文介绍了在Matlab中将strfind用于矩阵中的不同对角线方向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在有人问之前,这是一个较早问题的转发,但是由于它有答案,所以我不能删除它,因此我正在对其进行修改,以便希望Daniel R会回答!

Before anyone asks, this is a repost of an earlier question, but I cannot delete it because it has answers, so I am modifying it so that hopefully Daniel R will answer it!

我有一个数字网格,我想用strfind沿8个方向中的任何一个读取一串数字.我设法使那些非对角线的对象能够很好地工作,这是我一直在努力的对角线的对象(除了downRight,丹尼尔·R曾帮助过我,我对此非常感激)!

I have a grid of numbers and I want to read a string of numbers with strfind in any of the 8 directions. The non-diagonal ones I have managed to get to work fine, it is the diagonal ones that I have been struggling with (except for downRight which Daniel R helped me with earlier which I am very thankful for)!

这是代码:

A = [5,16,18,4,9;
    9,10,14,3,18;
    2,7,9,11,21;
    3,7,2,19,22;
    4,9,10,13,8]

for r = 1:5
    for diags = -5:5
        downRight = strfind(diag(A,diags)', [10,9,19]);
        if isempty(downRight) == 0;
            rowOfFirstNum = downRight(1)+max(-diags,0);
            columnOfFirstNum = downRight(1)+max(diags,0);
        end
        downLeft = strfind(diag(rot90(A),diags)', [11,2,9]);
        if isempty(downLeft) == 0;
            %rowOfFirstNum =
            %columnOfFirstNum =
        end
        upLeft = strfind(diag(rot90(A,2),diags)', [19,9,10]);
        if isempty(upLeft) == 0;
            %rowOfFirstNum =
            %columnOfFirstNum =
        end
        upRight = strfind(diag(rot90(A,3),diags)', [3,7,14,4]);
        if isempty(upRight) == 0;
            %rowOfFirstNum =
            %columnOfFirstNum =
        end
    end
end

downRight可以工作,但是我不确定如何使其他设备正常工作.只是要注意,要测试每个方向,需要将其他3个注释掉.

downRight works, but I am not sure of how to get the others to work properly. Just to note, to test each direction the other 3 need to be commented out.

谢谢.

推荐答案

亲自解决我的问题,可能我必须写一个答案:)

A question personally addressing me, probably I must write an answer :)

我没有执行所有4种情况,而是编写了一个一般情况.您已经注意到,可以使用rot90产生4种情况(rot90(X,0)不执行任何操作).

Instead of implementing all 4 cases, I wrote a generic case. As you already noticed, the 4 cases can be produced using rot90 (rot90(X,0) does nothing).

为了获得索引,我创建了一个包含行号和列号的网格.只需按照rot90diag的相同过程进行操作,即可查看将哪个索引移到了该位置.

To get the indices, I created a meshgrid which contains row- and columnnumbers. Simply put it through the same process of rot90 and diag, to see which index was moved to the position.

最后,外循环(for r = 1:5)仅重复所有内容.

Finally, the outer loop (for r = 1:5) simply repeats everything.

A = [5,16,18,4,9;
     9,10,14,3,18;
     2,7,9,11,21;
     3,7,2,19,22;
     4,9,10,13,8];
[col,row]=meshgrid(1:size(A,1));


 x=[10,9,19];
% x=[11,2,9];
% x=[19,9,10];
% x=[3,7,14,4];
for diags = -5:5
    for direction=0:3
        loc = strfind(diag(rot90(A,direction),diags)', x);
        if ~isempty(loc)
            colT=diag(rot90(col,direction),diags);
            rowT=diag(rot90(row,direction),diags);
            rowOfFirstNum=rowT(loc)
            columnOfFirstNum=colT(loc)
            switch direction
            case 0
                %code for downRight
            case 1
                %code for downLeft
            case 2
                %code for upLeft
            case 3
                %code for upRight
            end
        end
    end
end

这篇关于在Matlab中将strfind用于矩阵中的不同对角线方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 10:43