MATLAB Answers

how to vector with the same length of matrix

4 views (last 30 days)
Valerio Gianforte on 1 May 2020
Answered: Peter Perkins on 5 May 2020
Hi everyone,
I have to do a vector with the same lenght of matrix. This new vector should contain a package of hour from 1 to 8766, one time that it arrives to end point (8766) it must restarts from 1 and arrives to 8766 and this for the all length of the matrix. I hope to have been clear enough. Thank you.
File = 'D:\Valerio\data\Time_dir_Analysis\Analisys\data\a_ERA5.xlsx';
readFile = xlsread(File);
ERA5 = unique(readFile,'rows');
dt_ERA5 = datetime([ERA5(:,1:4) repmat([0 0],size(ERA5,1),1)]);
tt_ERA5 = timetable(dt_ERA5,ERA5(:,5:end));

1 Comment

Eva-Maria Weiss on 1 May 2020
I'm not sure, if I got the point. The problem is that size of Matrix is not devisible by 8766?
Maybe the mod function would help b = mod(a,m). Then you can create first your vector by means of repmat function as much as it 'fits'
(floor(size(Matrix,1)/8766))
into the matrix size and then concatenate the 'rest' as 1:b...

Sign in to comment.

Accepted Answer

Ameer Hamza on 1 May 2020
Edited: Ameer Hamza on 1 May 2020
Try something like this
n = size(ERA5,1);
hours_vector = mod((1:n)-1,8766)+1;
hours_vector is like this
hours_vector = [1 2 3 .. .. 8765 8766 1 2 3 .. .. 8765 8766 ..]

0 Comments

Sign in to comment.

More Answers (1)

Peter Perkins on 5 May 2020
I'm going to suggest that you stay away from xlsread, and use readtable instead. Putting that together with Am eer's answer, you have
ERA5 = readtable('a_ERA5.xlsx');
ERA5.Time = datetime(ERA5{:,1:3}) + hours(ERA5.Var4);
ERA5 = table2timetable(ERA5(:,5:end));
n = height(ERA5);
ERA5.Hours = hours(mod((1:n)-1,8766)+1)';
head(ERA5)
ans =
8×6 timetable
Time Var5 Var6 Var7 Var8 Var9 Hours
____________________ _____ _____ ______ ______ ______ _____
01-Jan-2005 00:00:00 0.652 8.216 66.211 1.371 -1.466 1 hr
01-Jan-2005 01:00:00 0.645 8.029 65.466 0.559 -2.62 2 hr
01-Jan-2005 02:00:00 0.635 7.915 64.978 -0.359 -3.037 3 hr
01-Jan-2005 03:00:00 0.622 7.81 64.896 -0.861 -2.831 4 hr
01-Jan-2005 04:00:00 0.608 7.702 64.785 -0.94 -2.648 5 hr
01-Jan-2005 05:00:00 0.594 7.593 64.776 -1.396 -2.675 6 hr
01-Jan-2005 06:00:00 0.582 7.484 64.675 -1.944 -2.773 7 hr
01-Jan-2005 07:00:00 0.569 7.369 64.665 -2.103 -2.613 8 hr
I don't actually understand what you want to do with the vector of "hours", but perhaps something like that.

0 Comments

Sign in to comment.

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by