【问题标题】:Matlab Matrix Repeat ValueMatlab 矩阵重复值
【发布时间】:2016-09-30 10:53:04
【问题描述】:

我的矩阵: e =

 1     2
 2     3
 3     3
 4     3
 5     2

我想在同一行中重复第一列的值与第二列的数字一样多。我想让我的矩阵像: e =

 1     2
 1     2
 2     3
 2     3
 2     3
 3     3
 3     3
 3     3
 4     3
 4     3
 4     3
 5     2
 5     2
 thank you for your help...

【问题讨论】:

  • 请修改您的问题。我不明白你在问什么。
  • 我需要将该矩阵中第一个列的值随机化到一个新的 (2,3,4) 矩阵中。所需的输出看起来像 ans(:,:,1) = 0 0 0 1 1 0 ans(:,:,2) = 2 2 2 0 0 0 ans(:,:,3) = 0 0 0 0 0 0 ans(:,:,4) = 0 0 0 0 0 0 1 将重复从第 3 列开始的数字,在这种情况下为 2。 2 不能与 1 -> (:, :,1) 并且必须放在另一层 ->(:,:,2) 等,因为这些数字在第二列具有相同的值。
  • 还是有点混乱,试着重写你的问题并添加更多示例。确保对其进行格式化以提高可读性
  • 对不起..好的,我会重写并重新安排我的问题..请稍候。非常感谢...
  • 你好,我的朋友@nilZ0r,我已经重写了我的问题,感谢所有回复:)

标签: matlab matrix


【解决方案1】:

您可以使用repelem 重复行索引,然后从e 中获取这些行:

new_e = e(repelem(1:size(e,1), e(:,2)), :);

如果您使用的是 2015a 之前没有 repelem 的 MATLAB 版本,这里有另一种方法:

spacing = cumsum([1; e(:,2)]);      % the rows of new_e where we change row values
row_indices(spacing) = 1;           % make a vector with these elements = 1
row_indices = cumsum(row_indices);  % convert to row indices, last index is invalid
new_e = e(row_indices(1:end-1), :); % select valid rows from e

【讨论】:

  • 我用的是matlab 2013b,除了repelem我还能用什么函数?@beaker
猜你喜欢
  • 1970-01-01
  • 2018-02-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-28
相关资源
最近更新 更多