How to use multiple arguments in matrix

조회 수: 3 (최근 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 CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by