Efficient way to build a matrix

9 views (last 30 days)
I am looking for a more elegant and efficient way to build a matrix that I need for some subsequent computations and plotting. I start with a matrix called IndexSunrise, which is 1:7000 and holds sequential values but spaced at irregular intervals, e.g. IndexSunrise = [12 45 93 141 194 ... 7000]. I want create a new matrix that includes these plus the 7 sequential values that preceed these, e.g. IndexSunriseFinal = [5 6 7 8 9 10 11 12 37 38 39 40 41 42 43 45 86 87 88 89 90 91 92 93 ... 7000].
Presently I have it like the below
Index1 = IndexSunrise;
Index2 = IndexSunrise-1;
Index3 = IndexSunrise-2;
Index4 = IndexSunrise-3;
Index5 = IndexSunrise-4;
Index6 = IndexSunrise-5;
Index7 = IndexSunrise-6;
IndexSunriseFinal = [Index1 Index2 Index3 Index4 Index5 Index6 Index7];
While this works, it's ugly and inneficient and makes debuging harder (i.e. if I want to look at the 8 or 9 preceeding values instead, I have to rebuild the above).
I'm sure there has to be a better way to do this without using something worse like eval.
Suggestion?

Accepted Answer

Atsushi Ueno
Atsushi Ueno on 19 May 2021
> I want create a new matrix that includes these plus the 7 sequential values that preceed these, e.g. IndexSunriseFinal = [5 6 7 8 9 10 11 12 37 38 39 40 41 42 43 45 86 87 88 89 90 91 92 93 ... 7000].
IndexSunrise = [12 45 93 141 194 7000];
n = 7;
IndexSunriseFinal = repelem(IndexSunrise, n+1) - repmat((n:-1:0), size(IndexSunrise));
>> repelem(IndexSunrise, n+1)
ans = 12 12 12 12 12 12 12 12 45 45 45 45 45 45 45 45 93 93 93 93 93 93 93 93 141 141 141 141 141 141 141 141 194 194 194 194 194 194 194 194 7000 7000 7000 7000 7000 7000 7000 7000
>> repmat((n:-1:0), size(IndexSunrise))
ans = 7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0
>> repelem(IndexSunrise, n+1) - repmat((n:-1:0), size(IndexSunrise))
ans = 5 6 7 8 9 10 11 12 38 39 40 41 42 43 44 45 86 87 88 89 90 91 92 93 134 135 136 137 138 139 140 141 187 188 189 190 191 192 193 194 6993 6994 6995 6996 6997 6998 6999 7000

More Answers (1)

Steven Lord
Steven Lord on 19 May 2021
Take advantage of implicit expansion.
IndexSunrise = [12 45 93 141 194 7000]
IndexSunrise = 1×6
12 45 93 141 194 7000
offsets = (-6:0).'
offsets = 7×1
-6 -5 -4 -3 -2 -1 0
values = IndexSunrise + offsets
values = 7×6
6 39 87 135 188 6994 7 40 88 136 189 6995 8 41 89 137 190 6996 9 42 90 138 191 6997 10 43 91 139 192 6998 11 44 92 140 193 6999 12 45 93 141 194 7000
reshape the values array if desired.
  1 Comment
David Velasco
David Velasco on 19 May 2021
Interesting. This works as well, and it's actually faster (by a tiny bit) than the first one:
Using repelem & and repmap: Elapsed time is 0.000327 seconds.
Using implicit expansion: Elapsed time is 0.000283 seconds.

Sign in to comment.

Categories

Find more on Dates and Time in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!