How to use multiple arguments in matrix

4 ビュー (過去 30 日間)
Piotr Janota
Piotr Janota 2020 年 11 月 15 日
回答済み: Peter Perkins 2020 年 11 月 19 日
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 件のコメント
dpb
dpb 2020 年 11 月 15 日
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."

サインインしてコメントする。

採用された回答

VBBV
VBBV 2020 年 11 月 16 日
%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;
hours((hours<=13&hours>=6)|(hours<=22&hours>=16))=0.5;
result = hours;
B = result*A;
Try the above

その他の回答 (1 件)

Peter Perkins
Peter Perkins 2020 年 11 月 19 日
Or
>> 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;

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by