在matlab中将每一行与另一行连接起来?

在matlab中将每一行与另一行连接起来?,matlab,matrix,Matlab,Matrix,我有一个矩阵4X4试图连接如下行: 1 with 2 1 with 3 1 with 4 2 with 3 2 with 4 3 with 4 最后,我分别有6行偶数 亲爱的((thefourtheye)),下面是最终代码的输出,如果您可以查看,我将重复一行 新线= 1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 4 1 1 1 1 2 2

我有一个矩阵4X4试图连接如下行:

1 with 2
1 with 3
1 with 4
2 with 3
2 with 4
3 with 4
最后,我分别有6行偶数

亲爱的((thefourtheye)),下面是最终代码的输出,如果您可以查看,我将重复一行

新线=

 1     1     1     1
 2     2     2     2
 3     3     3     3
 4     4     4     4
 1     1     1     1
 2     2     2     2
 4     4     4     4
 5     5     5     5
 1     1     1     1
 2     2     2     2
 5     5     5     5
 6     6     6     6
 2     2     2     2
 3     3     3     3
 4     4     4     4
 5     5     5     5
 2     2     2     2
 3     3     3     3
 5     5     5     5
 6     6     6     6
 3     3     3     3
 4     4     4     4
 5     5     5     5
 6     6     6     6
新线=

 1     1     1     1
 2     2     2     2
 3     3     3     3
 4     4     4     4
 1     1     1     1
 2     2     2     2
 4     4     4     4
 5     5     5     5
 1     1     1     1
 2     2     2     2
 5     5     5     5
 6     6     6     6
 2     2     2     2
 3     3     3     3
 4     4     4     4
 5     5     5     5
 2     2     2     2
 3     3     3     3
 5     5     5     5
 6     6     6     6
 3     3     3     3
 4     4     4     4
 5     5     5     5
 6     6     6     6
新线=

 1     1     1     1
 2     2     2     2
 3     3     3     3
 4     4     4     4
 1     1     1     1
 2     2     2     2
 4     4     4     4
 5     5     5     5
 1     1     1     1
 2     2     2     2
 5     5     5     5
 6     6     6     6
 2     2     2     2
 3     3     3     3
 4     4     4     4
 5     5     5     5
 2     2     2     2
 3     3     3     3
 5     5     5     5
 6     6     6     6
 3     3     3     3
 4     4     4     4
 5     5     5     5
 6     6     6     6
新线=

 1     1     1     1
 2     2     2     2
 3     3     3     3
 4     4     4     4
 1     1     1     1
 2     2     2     2
 4     4     4     4
 5     5     5     5
 1     1     1     1
 2     2     2     2
 5     5     5     5
 6     6     6     6
 2     2     2     2
 3     3     3     3
 4     4     4     4
 5     5     5     5
 2     2     2     2
 3     3     3     3
 5     5     5     5
 6     6     6     6
 3     3     3     3
 4     4     4     4
 5     5     5     5
 6     6     6     6
新线=

 1     1     1     1
 2     2     2     2
 3     3     3     3
 4     4     4     4
 1     1     1     1
 2     2     2     2
 4     4     4     4
 5     5     5     5
 1     1     1     1
 2     2     2     2
 5     5     5     5
 6     6     6     6
 2     2     2     2
 3     3     3     3
 4     4     4     4
 5     5     5     5
 2     2     2     2
 3     3     3     3
 5     5     5     5
 6     6     6     6
 3     3     3     3
 4     4     4     4
 5     5     5     5
 6     6     6     6
新线=

 1     1     1     1
 2     2     2     2
 3     3     3     3
 4     4     4     4
 1     1     1     1
 2     2     2     2
 4     4     4     4
 5     5     5     5
 1     1     1     1
 2     2     2     2
 5     5     5     5
 6     6     6     6
 2     2     2     2
 3     3     3     3
 4     4     4     4
 5     5     5     5
 2     2     2     2
 3     3     3     3
 5     5     5     5
 6     6     6     6
 3     3     3     3
 4     4     4     4
 5     5     5     5
 6     6     6     6

或多或少地手动

new = [old(1,:) old(2,:); old(1,:) old(3,:); old(1,:) old(4,:); old(2,:) old(3,:); old(2,:) old(4,:); old(3,:) old(4,:)]

但是如果您需要在其他场景中重复,您应该编写一个函数

您应该能够轻松地使用嵌套for循环执行此操作,然后根据需要存储它们,但我选择只将它们存储到单元格数组中

% Your original array is yourmat, it is square
newmat = [];
for n=1:length(yourmat)
for m=n+1:length(yourmat)
newline = yourmat(n, :) + yourmat(m, :); % Is this what you meant by concatenate?
newmat = [newmat; newline];
end
end
someMatrix = rand(4)
storage = {}
iter = 1
newMatrix = zeros(2,length(someMatrix));
for ii = 1:length(someMatrix)
    for jj = i+1:length(someMatrix)
        newMatrix(1,:) = someMatrix(ii,:);
        newMatrix(2,:) = someMatrix(jj,:);
        storage{iter} = newMatrix
        iter = iter + 1;
    end
end

在编辑之前,您在这篇文章上有一个[r]标签。R也是一种选择吗?如果是这样的话,你应该让它更明确,并从标题中删除matlab。使用for循环更好地连接到你,只有你知道你要做什么。连接以两种方式工作。但是记住DRY(),非常感谢在编辑了你的答案之后,我得到了关于n=1:size(yourmat)的答案,,,,,,,对于m=n+1:size(yourmat),,,,,,,,newline=[yourmat(n,:);yourmat(m,:),,,,,,,,end end end end end end,但是关于多行和多行我的意思是3行和3行,这实际上是我最初的答案,但我很快编辑了它,因为我以为你是指别的东西。多行可能很简单:换行符=[yourmat(n,:);yourmat(m,:)]仍然是,但是n和m现在是范围,即换行符=[yourmat(n:n+2,:);yourmat(m:m+2,:)]但是它会给我错误,因为矩阵的末尾没有n+2