Edoardo De Gaspari
Edoardo De Gaspari

Reputation: 55

Put data into Matrix

I'm using Matlab and i have this big vector representing time from 5 to 55 seconds sampled at 512 Hz,so each sample is aprox. 0.0020 sec. I want to fill a matrix in order that each row represents 10 sec. interval. i.e row1 5-15,row2 15-25,row3 25-35,row4 35-45 and row5 45-55 seconds. So i created a matrix 5 by 5121 elements,and i created this loop

Matrix=ones(5,5121)
tmp=1;
 for row=1:5
   for column=1:5121
    Matrix(row,column)=t(tmp);

    tmp=tmp+1;
   end 
end

The problem is that when i go to a new row i want that the last values of the previous row is repeated in the new row.

i.e

5-5.0020-5.0039...15

15-15.0020 and so on.

With this loop that i created i have this situation

5-5.0020-5.0039...15

15.0020-15.0039

Hope you can help me Thanks

Upvotes: 1

Views: 108

Answers (1)

Valerio Raco
Valerio Raco

Reputation: 183

n_rows = 5;
n_cols = 5121;
overlap_size = 10;

t = rand(n_rows*n_cols,1);
matrix = nan(n_rows,n_cols); 
i_t = 1;
 for i_row = 1 : n_rows
   for i_col = 1 : n_cols
    matrix(i_row,i_col) = t(i_t);
    i_t=i_t+1;
   end 
 end

matrix = [matrix(1:end-1,(end-overlap_size+1):end),matrix(2:end,:)];

of course, if this is the whole code, and you don´t have other operations in it, this is a far better solution:

n_rows = 5;
n_cols = 5121;
overlap_size = 10;
t = rand(n_rows*n_cols,1);
matrix = reshape(t,[n_cols,n_rows])';
matrix = [matrix(1:end-1,(end-overlap_size+1):end),matrix(2:end,:)];

Upvotes: 1

Related Questions