迭代MatLab中矩阵的对角元素 [英] Iterate over diagonal elements of a Matrix in MatLab

查看:93
本文介绍了迭代MatLab中矩阵的对角元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要获取矩阵中所有对角线的索引.矩阵可以是非正方形.

I need to get the indexes of all diagonals in a matrix. The matrix can be non square.

diag函数给出值,但是我需要坐标.因此,例如:[1 2 3; 4 5 6; 7 8 9],我想要[1 1; 2 2; 3;3]加上[2 6]3,因为它们是矩阵的上对角线,并且在下面(即[4 8]7)相同.因此,索引的完整列表为:[1 1; 2 2; 3 3], [1 2; 2 3], [1 3], [2 1], [3 2], [3 1].

The diag function gives the values, but I need the coords. So for instance with this: [1 2 3; 4 5 6; 7 8 9], I want [1 1; 2 2; 3;3] PLUS [2 6] and 3 because they are the upper diagonals of the matrix, and same for below i.e. [4 8] and 7. So the full list of indexes is: [1 1; 2 2; 3 3], [1 2; 2 3], [1 3], [2 1], [3 2], [3 1].

我也需要在另一个对角线方向上...

And I need this in the other diagonal direction too...

它需要适用于任何形状的矩阵,不仅是正方形的矩阵,还包括矢量(即1xn和nx1)-但我想我可以测试最后两种情况并分别对待.

And it needs to work for matrices of any shape i.e. not just square ones, including vectors (i.e. 1xn and nx1) - but I guess I can test for these last 2 cases and treat separately.

一个非正方形矩阵的示例是以下3x2矩阵:

An example of a non square matrix is this 3x2 one:

1   2
3   4
5   6

在这种情况下,我需要的索引是:

In this case, the indexes I need are:

[1 1; 2 2]
[2 1; 3 2]
[1 2]
[3 1]

然后以另一种方式运行对角线...

And then the same for diagonals running the other way...

我知道diag函数,可以编写一些代码来遍历矩阵,但是后来我看不到如何轻松地将这些值转换为索引.

I am aware of the diag function and can write a bit of code to iterate over the matrices, but I can't then see how to easily turn those values into indexes.

关于我为什么需要这个:这是我在MatLab上进行的在线课程.使用MatLab进行编程的简介.我是一位经验丰富的程序员,但对MatLab不熟悉,我想学习更多.

As for why I need this: It's an online course I'm doing in MatLab. An introduction to programming, using MatLab. I'm a reasonably experienced programmer but I'm not familiar with MatLab and I want to learn it more.

这个问题要求我们找到任何n数字在任何方向上的最大乘积,即行,列和对角线(双向).关键是我们不返回产品,我们返回组成该产品的元素的索引.

The question asks us to find the biggest product of any n numbers in any direction, i.e. rows, cols, and diagonals (both ways). And the thing is we don't return the product, we return the indexes of the elements making up this product.

我希望它能提供更多信息.

I hope this provides more information.

对不起,您还没回复您,我一直在忙着做其他事情.当我第一次在这里发布文章时,我以为我错过了一些东西,所以要花几行.但这比这要复杂得多,因此你们发布的代码将使我花更多的时间来理解和理解等等.但是我希望今天做这件事,并接受它作为答案.感谢您的所有答复.

EDIT 2: Sorry for not getting back to you guys, I've been very busy doing other things. When I first posted here, I assumed I was missing something and this would be something that took a few lines. But it's trickier than that, so the code you guys are posting will take me a little more time to go through and understand etc. But I hope to do this today and accept one as an answer. Thanks for all the replies.

推荐答案

好,所以昨晚我自己编写了此代码.这很长,我相信可以用更好的方法来完成.我对MatLab不太了解,所以我的解决方案很幼稚.但这有效.

Ok, so I coded this up myself last night. It's very long and I'm sure it can be done in better ways. I don't know MatLab well, so my solution is pretty naive. But it works.

function indicies = maxproduct(A, n)


function diagIndicies = getAllReverseDiagonalIndicies()
    % This function returns lists of indicies in the reverse diagonal direction (top-right to bottom-left)
    if rows == 1 
        numCells = 0;
        for j=1:length(A)
            temp = [1, j];
            numCells = numCells + 1;
            diagIndicies{numCells} = temp;
        end
        return
    end

    % This loop adds all diagonals down and to the right, to the end of the matrix.
    %fprintf('Diagonals down to main one are:\n');
    numCells = 0;
    for x=1:rows
        rowNum = x;
        colNum = 1;
        temp = [];
        while rowNum >= 1 && rowNum <= rows && colNum <= cols
            temp = [temp; [rowNum colNum]];
            rowNum = rowNum - 1;
            colNum = colNum + 1;
        end
        numCells = numCells + 1;
        temp = flipud(temp); % Need row major order for assignment
        %disp(temp);
        diagIndicies{numCells} = temp;
    end

    % Now go along bottom row
    %fprintf('Diagonals along bottom are:\n');
    for y=2:cols
        rowNum = rows;
        colNum = y;
        temp = [];
        while rowNum >= 1 && colNum <= cols
            temp = [temp; [rowNum colNum]];
            rowNum = rowNum - 1;
            colNum = colNum + 1;
        end
        numCells = numCells + 1;
        temp = flipud(temp); % Need row major order for assignment
        %disp(temp);
        diagIndicies{numCells} = temp;
    end                

end

function diagIndicies = getAllDiagonalIndicies()
    % This function returns lists of indicies in the main diagonal direction (top-left to bottom-right)
    if rows == 1 
        numCells = 0;
        for j=1:length(A)
            temp = [1, j];
            numCells = numCells + 1;
            diagIndicies{numCells} = temp;
        end
        return
    end

     % This loop adds all diagonals down and to the left, to the lhs of the matrix.
    %fprintf('Diagonals down to main one are:\n');
    numCells = 0;
    for x=1:rows
        rowNum = x;
        colNum = cols;
        temp = [];
        while rowNum >= 1 && rowNum <= rows && colNum >= 1
            temp = [temp; [rowNum colNum]];
            rowNum = rowNum - 1;
            colNum = colNum - 1;
        end
        numCells = numCells + 1;
        temp = flipud(temp); % Need row major order for assignment
        %disp(temp);
        diagIndicies{numCells} = temp;
    end

    % Now go along bottom row...
    %fprintf('Diagonals along bottom are:\n');
    for y=cols-1:-1:1
        rowNum = rows;
        colNum = y;
        temp = [];
        while rowNum >= 1 && colNum >= 1
            temp = [temp; [rowNum colNum]];
            rowNum = rowNum - 1;
            colNum = colNum - 1;
        end
        numCells = numCells + 1;
        temp = flipud(temp); % Need row major order for assignment
        %disp(temp);
        diagIndicies{numCells} = temp;
    end

 end


 % Main function starts here.

[rows, cols] = size(A);

theMaxProduct = -10000000;
indicies = [];

if isscalar(A)
    if A == 1 && n == 1
        indicies = [1,1];
        return;
    else
        return;
    end
end



% Find max product in each row of A
for i=1:rows
    for j=1:cols-n+1
        theProduct = 1;
        for k=j:j+n-1
            theProduct = theProduct * A(i, k);
        end
        if theProduct > theMaxProduct
            theMaxProduct = theProduct;
            indicies = [];
            for k=j:j+n-1
                indicies = [indicies; [i, k]];
            end

        end
    end
end
fprintf('theMaxProduct after examining rows = %15.15f\n', theMaxProduct);

% Find max product in each column of A
for i=1:cols
    for j=1:rows-n+1
        theProduct = 1;
        for k=j:j+n-1
            theProduct = theProduct * A(k, i);
        end
        if theProduct > theMaxProduct
            theMaxProduct = theProduct;
            indicies = [];
            for k=j:j+n-1
                indicies = [indicies; [k, i]];
            end

        end
    end
end
fprintf('theMaxProduct after examining cols = %15.15f\n', theMaxProduct);



% Find max product along reverse diagonals of A
diagIndicies = getAllReverseDiagonalIndicies();
%disp(diagIndicies);
for i=1: length(diagIndicies)
    [numIndicies, ~] = size(diagIndicies{i});
    for j=1:numIndicies-n+1
        theProduct = 1;
        for k=j:j+n-1
            theProduct = theProduct * A(diagIndicies{i}(k, 1), diagIndicies{i}(k, 2));
        end
        if theProduct > theMaxProduct
            theMaxProduct = theProduct;
            indicies = [];
            for k=j:j+n-1
                indicies = [indicies; [diagIndicies{i}(k, 1), diagIndicies{i}(k, 2)]];
            end

        end
    end
end
fprintf('theMaxProduct after examining reverse diag = %15.15f\n', theMaxProduct);


% Find max product along diagonals of A
diagIndicies = getAllDiagonalIndicies();
%disp(diagIndicies);
for i=1: length(diagIndicies)
    [numIndicies, ~] = size(diagIndicies{i});
    for j=1:numIndicies-n+1
        theProduct = 1;
        for k=j:j+n-1
            theProduct = theProduct * A(diagIndicies{i}(k, 1), diagIndicies{i}(k, 2));
        end
        if theProduct > theMaxProduct
            theMaxProduct = theProduct;
            indicies = [];
            for k=j:j+n-1
                indicies = [indicies; [diagIndicies{i}(k, 1), diagIndicies{i}(k, 2)]];
            end

        end
    end
end
fprintf('theMaxProduct after examining main diag = %15.15f\n', theMaxProduct);

结束

这篇关于迭代MatLab中矩阵的对角元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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