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