フィルターのクリア

how to repeat elements of column vector

4 ビュー (過去 30 日間)
Sara
Sara 2018 年 7 月 27 日
コメント済み: Sara 2018 年 7 月 30 日
I have a matrix of 40500*4 and which its first column is time stamp of 1 to 1500 (for example 300 rows have the value 1 for its time 500 rows have the value 2 and ..... I have another column vector which is 1500*1. what I want to do is to extend the size my column vector to 40500*1 (the same size with my matrix rows) in the way when the time value is 1 repeat the first element of my column vector for n times where the n is the number of rows in my matrix which have the value 1 in the time column. I wrote a code something like that but it takes ages to run. could you please help me to write a better code. Thanks
data = 40500*1
data2 =1500*1;
n=max(data=(:,1)); %%n=1500
m = size(data,1); %%%m=40500
x = cell(n,1)
for i =1:n
x{i}=find(dataCopy(:,1)==i); %%%x = 1500
end
newdata = cell(m,1);
for i=1:n
for j = 1:m
newdata{j}=repelem(data2(i),numel(x{i,1}));
end
end

採用された回答

Ben Frankel
Ben Frankel 2018 年 7 月 27 日
You can get a vector where V(i) = count of timestamp i, with:
V = diff([0; find(diff([data(:, 1); 1]))])
Then you can stretch your column vector C with:
stretched = repelem(C, V, 1);
  1 件のコメント
Sara
Sara 2018 年 7 月 30 日
Thanks a lot. your code is perfect.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

製品


リリース

R2018a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by