how to repeat elements of column vector
4 次查看(过去 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
0 个评论
采纳的回答
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);
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrices and Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!