MATLAB Answers

How to use multiple arguments in matrix

1 view (last 30 days)
Piotr Janota
Piotr Janota on 15 Nov 2020
Answered: Peter Perkins on 19 Nov 2020
I have quite long array 'A' (8760x1) of saved data. From 1 to 8760 (those are hours during a year). I need to set different values for certain hours.
I started from creating a new array 't' which will be set into 24-hour periods (days). Exactly from 0 to 23.
t = [1:8760]';
hours = rem(t,24);
now, what I need to do, is multiply the values (saved in 'A') so that they meet the conditions for specific hours that is:
  • 0,2 for hours: 23:00 to 6:00 and 14:00 to 15:00
  • 0,5 for hours: 6:00 to 13:00 and 16:00 to 22:00
result = (hours <6)*0.2 + (hours >=14)*0.2 + (hours <15)*0.2 + (hours >=23)*0.2 + (hours >=6)*0.5 + (hours >=16)*0.5
And the result will not work because the intervals intersect. Some ideas?
The final equation will be:
B = result*A

  1 Comment

dpb on 15 Nov 2020
Use a timeseries and things will be much simpler methinks.
It's not clear to me just what the issue is; you stated "multiply the values (saved in 'A') so that they meet the conditions for specific hours" but if the result is to just have 0.2 or 0.5 for the times given, why not just set the result?
Why does it have to be a multiplication and if there is some reason that is mandatory, then need to know what the content of A is in order for a multiplication to work.
If, otoh, what you're showing as multiplication by a logical expression in the above expression for result is what you mean, then that's the wrong way at it--just use logical addressing directly. Then, with a timeseries or time table or even with just a duration vector, there should be no intersection issue as your time intervals don't overlap (except for the breakpoints which you need to have probably as 2300 - 0559 and 0600 - 1259 instead.
You have to decide which value belongs to the beginning of the interval and the end of one interval cannot contain the beginning of the next. "Time--it's what keeps everything from happening all at once."

Sign in to comment.

Accepted Answer

VBBV on 16 Nov 2020
%if true
%if true
clear all
t = [1:8760]';
hours = rem(t,24);
hours((hours<6&hours>=0)|hours==23|(hours<=15&hours>=14)) = 0.2;
result = hours;
B = result*A;
Try the above


Sign in to comment.

More Answers (1)

Peter Perkins
Peter Perkins on 19 Nov 2020
>> t = datetime(2020,1,1,0:8759,0,0);
>> scale = [repmat(.2,1,6) repmat(.5,1,8) repmat(.2,1,2) repmat(.5,1,7) .2];
>> scale(t.Hour+1) .* A;


Sign in to comment.

Community Treasure Hunt

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

Start Hunting!