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

时间:2013-11-27 17:11:15

标签: matlab matrix

我尝试连接像这样的行的矩阵4X4:

1 with 2
1 with 3
1 with 4
2 with 3
2 with 4
3 with 4

最后我分别有6行

亲爱的((thefourtheye)),这下面是最终代码的输出我会重复一行,如果你可以复习

newline =

 1     1     1     1
 2     2     2     2
 3     3     3     3
 4     4     4     4

newline =

 1     1     1     1
 2     2     2     2
 4     4     4     4
 5     5     5     5

newline =

 1     1     1     1
 2     2     2     2
 5     5     5     5
 6     6     6     6

newline =

 2     2     2     2
 3     3     3     3
 4     4     4     4
 5     5     5     5

newline =

 2     2     2     2
 3     3     3     3
 5     5     5     5
 6     6     6     6

newline =

 3     3     3     3
 4     4     4     4
 5     5     5     5
 6     6     6     6

3 个答案:

答案 0 :(得分:1)

或多或少手动......

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,:)]

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

答案 1 :(得分:0)

% 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

答案 2 :(得分:0)

您应该可以使用嵌套for循环轻松完成此操作,然后根据需要存储它们,但我选择将它们存储到单元格数组中

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