簡體   English   中英

Matlab矩陣重復值

[英]Matlab Matrix Repeat Value

我的矩陣: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...

您可以使用repelem重復行索引,然后從e獲取這些行:

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

如果您使用此之前,沒有一個2015A MATLAB版本repelem ,這里是另一種方式來做到這一點:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM