Matlab:如何通过移动行来获取所有可能的不同矩阵(更新:每一行都有不同的步骤) [英] Matlab: How to get all the possible different matrices by shifting it's rows (Update: each row has a different step)

查看:98
本文介绍了Matlab:如何通过移动行来获取所有可能的不同矩阵(更新:每一行都有不同的步骤)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我说我有一个3x5的矩阵

Lets say I have a 3x5 matrix

a=[1 2 4 7 5;
   3 4 5 6 2;
   6 7 1 2 3];

我想通过移动行来获取所有不同的矩阵.

I want to get all the different matrices by shifting it's rows.

我写的代码是

a=[1,2,4,7,5;3,4,5,6,2;6,7,1,2,3];

for j=1:3

    for i = 1:5

        a(j,:)=circshift(a(j,:),[i 1]);
        disp(a)
    end
end

问题是我应该有25种不同的矩阵,但是我却 有15个(由于j的3个循环中i的5个循环)我该如何解决 它吗?

the problem is that I should have 25 different matrices but instead I have 15 (Due to the 5 loops of i in the 3 loops of j) How can I fix it?

是否有一般方法(不适用于特定大小的矩阵)

Is there a general way to do this (Not for matrices of specific size)


(编辑)


(EDIT)

我注意到这对于我的程序而言效率不高(对于10x24矩阵,结果非常庞大),因此我对程序进行了一些更改. 新问题:

I noticed that this was not efficient for my program (for a 10x24 matrix the results are huge) so I changed the program a bit. New problem:

让我说我有一个3x6的矩阵

Lets say I have a 3x6 matrix

a=[1 2 4 7 5 7;
   3 4 5 6 2 9;
   6 7 1 2 3 4];

我想通过移动行来获取所有不同的矩阵.但是诀窍是每一行都有不同的移位步骤. (通过不同矩阵中的值定义)

I want to get all the different matrices by shifting it's rows. But the trick is that every row has a different shift step. (defined from value inside a different matrix)

例如: 第1行的步长为3(2个不同的行): 1 2 4 7 5 7和7 5 7 1 2 4 第2行的步长为2(3个不同的行): 3 4 5 6 2 9、5 6 2 9 3 4和2 9 3 4 5 6 第3行的步长为3(2个不同的行): 6 7 1 2 3 4和2 3 4 6 7 1

For example: row 1 has a step of 3 (2 different rows): 1 2 4 7 5 7 and 7 5 7 1 2 4 row 2 has a step of 2 (3 different rows): 3 4 5 6 2 9 , 5 6 2 9 3 4 and 2 9 3 4 5 6 and row 3 has a step of 3 (2 different rows): 6 7 1 2 3 4 and 2 3 4 6 7 1

这意味着我应该有2x3x2 = 12个不同的矩阵.

this means that I should have 2x3x2 = 12 different matrices.

a=[1,2,4,7,5,7;3,4,5,6,2,9;6,7,1,2,3,4];
b=[2,3,2];

    for j=1:3
        if b(j) == 2
        for i = 1:2
            a(j,:)=circshift(a(j,:),[i 3]);
            disp(a)
        end
        elseif b(j) == 3
        for i = 1:3
            a(j,:)=circshift(a(j,:),[i 2]);
            disp(a)
        end
    end

有没有办法以3x6x12矩阵或(3 * 12)x6矩阵获取结果?

Is there a way to get my results in a 3x6x12 matrix or a (3*12)x6 matrix?

推荐答案

这样的递归函数如何:

function res = shift_all(a)
res = shift_recurse(a,1,[]);

function res = shift_recurse(a,r,existing)
res = existing;
m = a;
for i = 1:size(a,2)
    if r < size(a,1)
        res = shift_recurse(m, r+1, res);
    elseif r == size(a,1)
        res = cat(3, res, m);
    end
    m(r,:) = circshift(a(r,:),[1 i]);
end

调用res = shift_all(a)会生成3x5x125的结果矩阵.

And calling res = shift_all(a) yields a 3x5x125 matrix of results.

这篇关于Matlab:如何通过移动行来获取所有可能的不同矩阵(更新:每一行都有不同的步骤)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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