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

采纳的回答

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);

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrices and Arrays 的更多信息

产品


版本

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by