how to repeat elements of column vector
4 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponse acceptée
Ben Frankel
le 27 Juil 2018
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);
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Matrices and Arrays dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!