【问题标题】:Append sequential columns [closed]附加顺序列[关闭]
【发布时间】:2018-01-24 21:46:09
【问题描述】:

我有一个 50 x 100 的数组大小。我想做的是将第二列附加到第一列,将第四列附加到第三列,将第六列附加到第 5 列等等......这样我就有了100 x 50 矩阵。以下示例显示了我正在尝试做的事情

1 2 3 4 变成 1 3 以此类推 5 6 7 8 5 7 2 4 6 8

我找了一个类似的问题,但找不到一个

【问题讨论】:

  • 你做了什么?请注意,堆栈溢出需要您付出一些努力,这意味着在这种情况下考虑如何继续,无论是伪代码还是实际代码,在这种情况下它被称为minimal reproducible example。列出你得到的任何输入、期望的输出和错误。请参阅How to Ask

标签: matlab append reshape


【解决方案1】:

这是一个小例子:

% Define a sample matrix:
A = [
  1 2 3 4 5 6 7 8 9 10 11 12;
  1 2 3 4 5 6 7 8 9 10 11 12;
  1 2 3 4 5 6 7 8 9 10 11 12;
  1 2 3 4 5 6 7 8 9 10 11 12;
  1 2 3 4 5 6 7 8 9 10 11 12;
  1 2 3 4 5 6 7 8 9 10 11 12
];

% Build an index to even rows:
idx_even = mod(1:size(A,2),2) == 0;

% Store the even rows of the matrix in a saparate variable:
A_even = A(:,idx_even);

% Delete the even rows from the original matrix:
A(:,idx_even) = [];

% Append the even rows to the remaining (odd) rows of the original matrix:
A = [A; A_even];

输出:

A =

     1     3     5     7     9    11
     1     3     5     7     9    11
     1     3     5     7     9    11
     1     3     5     7     9    11
     1     3     5     7     9    11
     1     3     5     7     9    11
     2     4     6     8    10    12
     2     4     6     8    10    12
     2     4     6     8    10    12
     2     4     6     8    10    12
     2     4     6     8    10    12
     2     4     6     8    10    12

【讨论】:

    猜你喜欢
    • 2015-07-12
    • 1970-01-01
    • 1970-01-01
    • 2021-08-08
    • 2021-07-19
    • 1970-01-01
    • 2013-11-12
    • 2020-09-25
    • 1970-01-01
    相关资源
    最近更新 更多