Which function will cause less load to Matlab?

1 view (last 30 days)
By load, I mean the processing time Matlab needs.
Having 3000 lines of if statements where the statement is constant and only the condition is varied or to use a while(1) with a loop with a evalc and sprintf
while(1) is needed in the loop because it needs to be compared all the time.

Accepted Answer

ericson
ericson on 14 Mar 2014
Why is it 8 buildings?
ssn = @(sdn) round(24*3600*sdn); iso_week_day = @(sdn) 1 + rem( weekday( sdn - 2 ), 7 ); -I tried running this program in my 2011 version but I can't seem to find what is the function of this code?
I can't also understand this for iwd = 1 :7 for ts = 1 : 12 time_slot_array(ts,1,iwd) = ssn(datenum( vec + [0,0,iwd-1,7+ts, 0, 0] )); time_slot_array(ts,2,iwd) = ssn(datenum( vec + [0,0,iwd-1,7+ts,59,59] )); end end -why do you need an to add an initial date to the 2 loops?
for bb = 1 : 8 for ff = 1 : 4 for rr = 1 : 30 Building(bb).Floor(ff).Room(rr).TimeSlot = time_slot_array; end end end -There should be a specific matrix area right? i.e. time_slot_array(2,1,7)
  1 Comment
per isakson
per isakson on 14 Mar 2014
Edited: per isakson on 14 Mar 2014
Did you run my code? Answers:
  • "8" - I just picked a number that I thought was reasonable. Didn't we discuss "buildings" in plural? If it is only one building then use Floor(ff).Room(rr).TimeSlot
  • anonymous functions have been around for ten years or more, I guess. round, rem|and |wwekday even longer. Did it throw any error? See http://www.mathworks.se/help/matlab/matlab_prog/anonymous-functions.html
ssn = @(sdn) round(24*3600*sdn);
iso_week_day = @(sdn) 1 + rem( weekday( sdn - 2 ), 7 );
  • I assumed that a time slot has a start and an end point in time. [0,0,iwd-1,7+ts, 0, 0] provides the start and [0,0,iwd-1,7+ts,59,59] the end (59 minutes and 59 seconds later).
  • The code before %% Evaluate red/green ... is just a simple (/cheap) way to provide some dumb sample data.
  • "-why do you need an to add an initial date to the 2 loops?" To indicate that time-slot-data for any week can be generated by changing the value of vec
  • time_slot_array(2,1,7) - I assigned copies of the same data, which covers one week (starting at 2014-03-03 00:00:00), to all rooms (cheap sample data). (Second though: Matlab is smart and took probably advantage of the fact the all rooms had copies of the same data. With different data somewhat more time is probably needed for the comparison.)

Sign in to comment.

More Answers (1)

per isakson
per isakson on 10 Mar 2014
Edited: per isakson on 13 Mar 2014
Both alternative appears a bit problematic
Make an experiment. There is a button [Run and Time].
There must be a third way. Give us a little more background.
.
Step 1. Data structure. What output do you want from your program? That is important to know to choose an appropriate data structure. Maybe, I'm guessing, a "nested" Matlab struct array. "ON" is represented by true and OFF by false. These lines initiate such a struct
clear('day','room')
day = struct( 'slot', false( 1, 12 ) );
room( 1, 120 ) = struct( 'day', repmat( day, [1,7] ) );
Now each one of the "10,080" slots can be accessed in a logical way, e.g.
>> room(45).day(5).slot(9)=true
room =
1x120 struct array with fields:
day
>> room(45).day(5).slot(9)
ans =
1
A logical array might be a better alternative.
What is the input (ON/OFF from the rooms) to the system and what kind of output is required? "Having 3000 lines of if statements [...]" is that an m-file or some kind of output from a building automation system?
.
A timing experiment:
Evaluating the button-color for
  • 8 buildings
  • 4 floors
  • 30 rooms
takes less than 0.04 seconds with my five years old vanilla desktop.
.
All the slot information is stored in a structure array:
Building(8).Floor(4).Room(30).TimeSlot
The field TimeSlot holds all time slot data for one week. (I have not included the check-boxes - yet.)
Initiate one time_slot_array with some simple data
vec = datevec( '2014-03-03 00:00:00' );
time_slot_array = nan( 12, 2, 7 ); % time_slot_array(:,1,:) 1 is begin, 2 end
ssn = @(sdn) round(24*3600*sdn);
iso_week_day = @(sdn) 1 + rem( weekday( sdn - 2 ), 7 );
for iwd = 1 :7
for ts = 1 : 12
time_slot_array(ts,1,iwd) = ssn(datenum( vec + [0,0,iwd-1,7+ts, 0, 0] ));
time_slot_array(ts,2,iwd) = ssn(datenum( vec + [0,0,iwd-1,7+ts,59,59] ));
end
end
Assign the same time_slot_array to all rooms
Building(8).Floor(4).Room(30).TimeSlot = time_slot_array;
is_red = false( 4, 4, 30 );
for bb = 1 : 8
for ff = 1 : 4
for rr = 1 : 30
Building(bb).Floor(ff).Room(rr).TimeSlot = time_slot_array;
end
end
end
Evaluate red/green for all rooms (all will get the color)
cur_sdn = datenum( '2014-03-05 10:52:00' ); % current time
cur_ssn = ssn( cur_sdn );
tic
for bb = 1 : 8
for ff = 1 : 4
for rr = 1 : 30
iwd = iso_week_day( cur_sdn );
is1 = cur_ssn >= Building(bb).Floor(ff).Room(rr).TimeSlot(:,1,iwd);
is2 = cur_ssn <= Building(bb).Floor(ff).Room(rr).TimeSlot(:,2,iwd);
is_red( bb, ff, rr ) = any( is1 & is2 );
end
end
end
toc
outputs
Elapsed time is 0.032368 seconds.
  18 Comments
ericson
ericson on 13 Mar 2014
In what way should I do it so that I can compare it every second? Right now this is my main problem.
Time slot is the set of start time and end time (i.e. 12:00-13:00). The 12 time slots per day should not overlap each other. Assuming the program will be set for the first time, all time slots are set to 00:00-00:00. After they are edited to the time ranges that you want, just click the apply button and it will be saved to the mat file. I already have a code which updates the mat file loaded in the PROGRAM gui when a change in the time in any room happened.
Sorry if I still didn't give you the answer that you want. I don't actually understand your question "When and by who are the actual values set? Which constraints on the values are there."
per isakson
per isakson on 13 Mar 2014
Edited: per isakson on 13 Mar 2014
I've added a timing experiment to the answer above. The structure Building may
  • be made persistent in the main function.
  • saved to a version '-v6' mat-file as needed
I represent time with floating-point integer to avoid rounding errors. ssn stands for serial second number.

Sign in to comment.

Categories

Find more on Specifying Target for Graphics Output 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!