How to adjust a variable via dividing it by the sum of observations in previous days? - date

I want to write a code to run the formula which is shown below:
Adjusted volume for each name and date = IntradayVolume / ( dailyvolume for five last days)
And I use codes which are shown below:
*Division for calculating adjusted volume;
proc sort data=sampledata_sumvolso02 out=sampledata_sumvolso02;
by TRD_STCK_CD TRD_EVENT_DT;
run;
*Adjusted intraday volume for days of a week;
data sampledata_adjvol02;
do until(last.TRD_STCK_CD);
do until(last.TRD_EVENT_DT);
set sampledata_sumvolso02;
by TRD_STCK_CD TRD_EVENT_DT;
if first.TRD_STCK_CD then
n=0;
if first.TRD_EVENT_DT then
n+1;
if n>7 then
do;
if not missing(IntradayVolume) then
adjusted_volume=divide(IntradayVolume,temp);
else call missing(adjusted_volume);
end;
if last.TRD_EVENT_DT then
temp=dailyvolume;
output;
end;
end;
drop temp n;
run;
Here is a sample of my data:
data WORK.SAMPLEDATA_SUMVOLSO02;
infile datalines dsd truncover;
input TRD_STCK_CD:$15. TRD_PR:32. TRD_TUROVR:14. TRD_EVENT_DT:DATE9. TRD_EVENT_TM:TIME5. TRD_EVENT_ROUNDED:32. TRD_EVENT_ROUFOR:$5. CountedVOLUME:32. DailyVolume:32. IntradayVolume:32.;
format TRD_TUROVR 14. TRD_EVENT_DT DATE9. TRD_EVENT_TM TIME5.;
label TRD_STCK_CD="TRD_STCK_CD" TRD_PR="TRD_PR" TRD_TUROVR="TRD_TUROVR" TRD_EVENT_DT="TRD_EVENT_DT";
datalines4;
BALI1,850,9260,24MAR2008,9:14,34200,9:30,7871000,,
BALI1,850,2000,23MAR2008,9:15,34200,9:30,1700000,,
BALI1,850,10000,22MAR2008,9:15,34200,9:30,8500000,,
BALI1,850,6000,21MAR2008,9:15,34200,9:30,5100000,,
BALI1,850,10000,20MAR2008,9:29,34200,9:30,8500000,31671000,31671000
BANK1,1164,10729,20MAR2008,9:38,36000,10:00,12488556,,12488556
BANK1,1148,2000,21MAR2008,11:24,41400,11:30,2296000,,
BANK1,1147,1575,22MAR2008,11:24,41400,11:30,1806525,16591081,4102525
BHMN1,1013,1500,14MAR2008,9:00,34200,9:30,1519500,,
BHMN1,1013,1500,15MAR2008,9:00,34200,9:30,1519500,,
BHMN1,1013,1500,16MAR2008,9:00,34200,9:30,1519500,,
BHMN1,1013,1500,17MAR2008,9:00,34200,9:30,1519500,,
BHMN1,1013,1500,18MAR2008,9:00,34200,9:30,1519500,,
BHMN1,1013,1500,19MAR2008,9:00,34200,9:30,1519500,,
BHMN1,1013,1000,20MAR2008,9:00,34200,9:30,1013000,,
BHMN1,1013,450,21MAR2008,9:00,34200,9:30,455850,,
BHMN1,1013,1500,22MAR2008,9:00,34200,9:30,1519500,,
BHMN1,1013,1500,23MAR2008,9:00,34200,9:30,1519500,,
BHMN1,1013,1500,24MAR2008,9:04,34200,9:30,1519500,,
BHMN1,1013,1500,25MAR2008,9:04,34200,9:30,1519500,,
BHMN1,1013,601,26MAR2008,9:05,34200,9:30,608813,,
BHMN1,1013,697,27MAR2008,9:06,34200,9:30,706061,,
BHMN1,1013,1500,28MAR2008,9:08,34200,9:30,1519500,,
BHMN1,1013,1500,29MAR2008,9:08,34200,9:30,1519500,,
BHMN1,1013,1500,12MAR2008,9:09,34200,9:30,1519500,,
BHMN1,1013,1500,13MAR2008,9:16,34200,9:30,1519500,,
BHMN1,1013,1500,14MAR2008,9:22,34200,9:30,1519500,,25576224
BHMN1,1013,1500,15MAR2008,9:41,36000,10:00,1519500,,1519500
BHMN1,1013,1500,16MAR2008,10:13,37800,10:30,1519500,,
BHMN1,1013,1500,17MAR2008,10:13,37800,10:30,1519500,,3039000
BHMN1,1013,1500,18MAR2008,11:25,41400,11:30,1519500,,1519500
BHMN1,1013,1500,19MAR2008,11:51,43200,12:00,1519500,33173724,1519500
CHML1,12000,745,20MAR2008,9:00,34200,9:30,8940000,,
CHML1,12000,82,21MAR2008,9:00,34200,9:30,984000,,
CHML1,11998,206,22MAR2008,9:07,34200,9:30,2471588,,
CHML1,11998,414,23MAR2008,9:09,34200,9:30,4967172,,
CHML1,11998,348,24MAR2008,9:12,34200,9:30,4175304,,
CHML1,11996,82,25MAR2008,9:15,34200,9:30,983672,,
CHML1,11996,1240,26MAR2008,9:16,34200,9:30,14875040,,
CHML1,11996,414,27MAR2008,9:17,34200,9:30,4966344,,
CHML1,11970,1000,28MAR2008,9:21,34200,9:30,11970000,,
CHML1,11964,158,29MAR2008,9:22,34200,9:30,1890312,,
CHML1,11990,1946,20MAR2008,9:23,34200,9:30,23332540,,79555972
CHML1,11851,500,19MAR2008,10:42,39600,11:00,5925500,,
CHML1,11852,1054,18MAR2008,10:44,39600,11:00,12492008,,
CHML1,11900,2000,17MAR2008,10:56,39600,11:00,23800000,,42217508
CHML1,11913,244,16MAR2008,11:08,41400,11:30,2906772,,
CHML1,11913,56,15MAR2008,11:09,41400,11:30,667128,,
CHML1,11968,944,14MAR2008,11:13,41400,11:30,11297792,,
CHML1,11968,2056,13MAR2008,11:27,41400,11:30,24606208,,
CHML1,11987,380,14MAR2008,11:29,41400,11:30,4555060,,44032960
CHML1,11988,620,11MAR2008,11:30,43200,12:00,7432560,,
CHML1,11981,1663,12MAR2008,11:52,43200,12:00,19924403,,
CHML1,11981,765,24MAR2008,11:52,43200,12:00,9165465,202328868,36522428
CRBN1,1486,1700,22MAR2008,9:00,34200,9:30,2526200,,
CRBN1,1485,3353,24MAR2008,9:21,34200,9:30,4979205,7505405,7505405
DADE1,1685,2972,24MAR2008,10:04,37800,10:30,5007820,,5007820
DADE1,1632,2000,20MAR2008,11:49,43200,12:00,3264000,,
DADE1,1631,5000,21MAR2008,11:49,43200,12:00,8155000,,
DADE1,1630,3000,22MAR2008,11:49,43200,12:00,4890000,,
DADE1,1630,2000,23MAR2008,11:50,43200,12:00,3260000,,
DADE1,1630,8000,24MAR2008,11:50,43200,12:00,13040000,37616820,32609000
DFRB1,7450,124,24MAR2008,10:54,39600,11:00,923800,,923800
DFRB1,7450,132,24MAR2008,11:16,41400,11:30,983400,1907200,983400
DJBR1,7491,3000,24MAR2008,10:06,37800,10:30,22473000,22473000,22473000
DRZK1,14890,72,24MAR2008,11:58,43200,12:00,1072080,1072080,1072080
FIBR1,2846,100,24MAR2008,11:17,41400,11:30,284600,284600,284600
FKAS1,3584,5173,20MAR2008,9:15,34200,9:30,18540032,,
FKAS1,3584,3000,21MAR2008,9:15,34200,9:30,10752000,,
FKAS1,3585,1827,22MAR2008,9:15,34200,9:30,6549795,,35841827
FKAS1,3585,183,23MAR2008,9:35,36000,10:00,656055,,
FKAS1,3585,3200,24MAR2008,9:35,36000,10:00,11472000,,12128055
FKAS1,3539,2000,25MAR2008,11:02,41400,11:30,7078000,,
FKAS1,3538,8000,26MAR2008,11:02,41400,11:30,28304000,,
FKAS1,3538,2000,27MAR2008,11:02,41400,11:30,7076000,,
FKAS1,3537,8000,28MAR2008,11:02,41400,11:30,28296000,,
FKAS1,3537,2000,29MAR2008,11:02,41400,11:30,7074000,,
FKAS1,3535,8000,20MAR2008,11:02,41400,11:30,28280000,,
FKAS1,3535,2000,19MAR2008,11:03,41400,11:30,7070000,,
FKAS1,3535,6617,18MAR2008,11:03,41400,11:30,23391095,,
FKAS1,3533,100,17MAR2008,11:03,41400,11:30,353300,,
FKAS1,3533,1283,16MAR2008,11:07,41400,11:30,4532839,,
FKAS1,3533,2763,15MAR2008,11:08,41400,11:30,9761679,,
FKAS1,3529,10000,14MAR2008,11:15,41400,11:30,35290000,,
FKAS1,3516,10000,13MAR2008,11:17,41400,11:30,35160000,,
FKAS1,3516,10000,12MAR2008,11:19,41400,11:30,35160000,,
FKAS1,3516,39,11MAR2008,11:19,41400,11:30,137124,,
FKAS1,3515,2000,19MAR2008,11:25,41400,11:30,7030000,,
FKAS1,3515,198,24MAR2008,11:25,41400,11:30,695970,,
FKAS1,3515,9802,20MAR2008,11:25,41400,11:30,34454030,,299144037
FKAS1,3515,4000,21MAR2008,11:48,43200,12:00,14060000,,
FKAS1,3513,6000,22MAR2008,11:52,43200,12:00,21078000,,
FKAS1,3514,2000,23MAR2008,11:52,43200,12:00,7028000,,
FKAS1,3514,1085,24MAR2008,11:52,43200,12:00,3812690,,
FKAS1,3500,1000,25MAR2008,11:56,43200,12:00,3500000,396592609,49478690
FOLD1,3198,390,24MAR2008,9:09,34200,9:30,1247220,,1247220
FOLD1,3197,2000,27MAR2008,10:31,39600,11:00,6394000,,
FOLD1,3191,2000,28MAR2008,10:32,39600,11:00,6382000,,
FOLD1,3190,4000,20MAR2008,10:33,39600,11:00,12760000,,
FOLD1,3190,542,18MAR2008,10:33,39600,11:00,1728980,,
;;;;
Now, I want to divide Intradayvolume variable by aggregated Dailyvolume variable in a week before today. It is important to specify for SAS to diagnoses days of a week and adapts it with the date variable in my dataset. Because the format of the date variable of my dataset is ddmmyy. I mean SAS should find which days of a week are today and aggregates Dailyvolume variable of a week before today as the denominator.
How can I run that formula?
Thanks in advance.

I'm not sure exactly what you're doing there, but one approach might be to create a temporary array and index it with the date. Then look up in that index. You'd have some issues with that simplistic of a method with holidays and such, but it should give you at least an approximately right answer.
For example (again, I don't entirely understand your variables or what you're doing, so this is an approximate idea):
*Adjusted intraday volume for days of a week;
data sampledata_adjvol02;
array prevdat[32767] _temporary_;
do until(last.TRD_STCK_CD);
do until(last.TRD_EVENT_DT);
set sampledata_sumvolso02;
by TRD_STCK_CD TRD_EVENT_DT;
if first.TRD_STCK_CD then
n=0;
if first.TRD_EVENT_DT then
n+1;
if n>7 then
do;
if not missing(IntradayVolume) then
adjusted_volume=divide(IntradayVolume,prevdat[TRD_EVENT_DT-7]);
else call missing(adjusted_volume);
end;
if last.TRD_EVENT_DT then
temp=dailyvolume;
output;
prevdat[TRD_EVENT_DT] = sum(prevdat[TRD_EVENT_DT],countedVolume);
end;
end;
call missing(of prevdat[*]);
drop temp n;
run;

data sampledata_adjvol (drop=cnt dv_:);
set Sampledata_SumVolSo;
by TRD_STCK_CD TRD_EVENT_DT;
retain dv_1 dv_2 dv_3 dv_4 cnt 0 dv_sum;
if first.TRD_STCK_CD then do;
cnt=0;
dv_1=0;
dv_2=0;
dv_3=0;
dv_4=0;
dv_sum=0;
end;
adjusted_volume_5=intradayvolume/dv_sum;
if last.TRD_EVENT_DT then do;
cnt=cnt+1;
if cnt=1 then dv_4=dailyvolume;
else if cnt=2 then dv_3=dailyvolume;
else if cnt=3 then dv_2=dailyvolume;
else if cnt=4 then dv_1=dailyvolume;
else do;
dv_sum=dailyvolume+dv_1+dv_2+dv_3+dv_4;
dv_4=dv_3;
dv_3=dv_2;
dv_2=dv_1;
dv_1=dailyvolume;
end;
end;
run;

Related

Calculation of the number of microseconds in Ada

in C langage we have get_usec() which gives us the number of microseconds since the start of the current second.
-Speaking of the "current second" necessarily refers to time reference which is often EpochTime.
-In Ada.Calendar package, I see Seconds or Clocks functions by example with ability to split & get the seconds.
But how to get the number of microseconds since the start of the current second, please?
Thanks
Mark
Note that Ada.Calendar is for local time, and may jump backwards. If it's available (are there any post-83 compilers that don't provide it?), you'll be better off using Ada.Real_Time ARM D.8:
Now : constant Ada.Real_Time.Time := Ada.Real_Time.Clock;
Count : Ada.Real_Time.Seconds_Count;
Sub : Ada.Real_Time.Time_Span;
...
Ada.Real_Time.Split (T => Now, SC => Count, TS => Sub);
Now Count contains the number of whole seconds since the epoch and Sub contains the fraction of a second in addition to Count. Ada.Real_Time.To_Duration converts a Time_Span to Duration, allowing you to multiply it by 1E6 to get microseconds.
The packages Ada.Calendar and Ada.Calendar.Formatting provide the information you will need.
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Calendar; use Ada.Calendar;
with Ada.Calendar.Formatting; use Ada.Calendar.Formatting;
procedure Main is
Now : Time := Clock;
Seconds : Second_Duration := Sub_Second (Now);
begin
Put_Line
("Sub seconds since current second: " &
Second_Duration'Image (Seconds));
end Main;
The output of one execution of this program is:
Sub seconds since current second: 0.655316600
In this execution the value indicated 655316.6 microseconds.
It can also be done (of course) without Ada.Calendar.Formatting, like this for example:
with Ada.Calendar; use Ada.Calendar;
...
type Seconds_In_Day is range 0 .. 86_400;
-- Or use Integer if it is 32 bits.
Now : constant Day_Duration := Seconds (Clock);
Subsec : Duration := Now - Day_Duration (Seconds_In_Day (Now));
...
if Subsec < 0.0 then
-- Conversion of Now rounded up instead of down.
Subsec := Subsec + 1.0;
end if;
with the result in Subsec.
But using Ada.Calendar.Formatting.Sub_Second is shorter, and may be better (faster or more accurate) for all I know; I did not compare the two methods.
Many thaks for yours answers.
Using all yours examples, i made some trials, one is below :
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Calendar; use Ada.Calendar;
with Ada.Real_Time; use Ada.Real_Time;
procedure Display_Current_Year is
--need to precise the origin package Ada.Real-Time else ambiguous
Now : Ada.Calendar.Time := Clock;
Now_Year : Year_Number;
Now_Month : Month_Number;
Now_Day : Day_Number;
Now_Seconds : Day_Duration;
Current_Real_Time : Ada.Real_Time.Time;
Time_Span : Ada.Real_Time.Time_Span;
Seconds_Count : Ada.Real_Time.Seconds_Count;
Hour : float;
begin
--- Ada.Calendar
Split (Now,
Now_Year,
Now_Month,
Now_Day,
Now_Seconds);
Put_Line("Calendar : Date du jour = ");
Put_Line ("Current year is: "
& Year_Number'Image (Now_Year));
Put_Line ("Current month is: "
& Month_Number'Image (Now_Month));
Put_Line ("Current day is: "
& Day_Number'Image (Now_Day));
Put_Line ("'Current' seconde is: "
& Day_Duration'Image (Now_Seconds));
New_Line;
--Ada.Real_Time;
Current_Real_Time := Ada.Real_Time.Clock;
Ada.Real_Time.Split (T => Current_Real_Time,
Sc => Seconds_Count,
Ts => Time_Span);
Put_Line ("Real_Time : Seconds_Count = " & Seconds_Count'Img);
Hour := (float(Seconds_count) / 3600.00);
Put_Line ("Hour since seconds origin : "
& (Hour'Img));
end Display_Current_Year;
with result :
$ ./display_current_year
Calendar : Date du jour =
Current year is: 2022
Current month is: 2
Current day is: 27
'Current' seconde is: 68625.325897000
Real_Time : Seconds_Count = 30953
Hour since seconds origin : 8.59806E+00
$
-Results for calendar are OK, but why 30953 seconds !!
Where does GNAT take the Epoch, if this is, in this case, please?
Thanks
Mark
You can do a dirty trick where you define a record My_raw_duration_Type : whole_part, fraction_part, both U32. Define Unchecked_Conversion To_Raw (Ada.Real_Time.Duration, My_Raw_Duration_Type). Then take the result of that and call it My_Raw_Duration. The milliseconds result you want is integer(float(My_Raw_Duration.Fraction_Part)/float(4*1032**2) * 1000.0);

Trouble Importing Zip Files with A Nested Do-Loop in SAS

I am attempting to import 104 zip files, each with approximately 30 excel files. I have created an excel file with all of the names of the zip files that I want to import (NYISO_Names). The first do-loop looks through all of the names of the zip files. Next, I created a list of the contents of each zip files. The second do-loop cycles through the contents.
So far, the code imports the first zip file (30 excel workbooks); however, it never moves on to the remaining zip files. Is something wrong with how the do-loops are nested?
%macro test;
%do i=1 %to 6;
%put ---&i.;
%let Iteration = &i.;
data abs;
set NYISO_Names;
if _n_ =&i. then call symput("FileName", strip(FileName));
if _n_ = &i. then call symput("Path", strip(Path));
run;
filename inzip zip "G:Desktop\&FileName.";
/* Read the "members" (files) from the ZIP file */
data Contents_&i.(keep=memname isFolder sheet);
length memname $200 isFolder 8;
fid=dopen("inzip");
if fid=0 then
stop;
memcount=dnum(fid);
do i=1 to memcount;
memname=dread(fid,i);
sheet = substr(memname, 1, length(memname)-4);
/* check for trailing / in folder name */
isFolder = (first(reverse(trim(memname)))='/');
output;
end;
rc=dclose(fid);
run;
DATA _NULL_; IF 0 THEN SET Contents_1 NOBS=X; CALL SYMPUT('RECCOUNT',X); STOP; RUN;
%do i = 1 %to &RECCOUNT;
data abs2;
set Contents_&Iteration.;
if _n_ =&i. then call symput("memname", strip(memname));
if _n_ =&i. then call symput("sheet", strip(sheet));
run;
/* identify a temp folder in the WORK directory */
filename xl "%sysfunc(getoption(work))/&memname." ;
/* hat tip: "data _null_" on SAS-L */
data _null_;
/* using member syntax here */
infile inzip(&memname.)
lrecl=256 recfm=F length=length eof=eof ;
file xl lrecl=256 recfm=N;
input;
put _infile_ $varying256. length;
return;
eof:
stop;
run;
proc import datafile=xl dbms=csv out=Test&i. replace;
run;
%end;
%end;
%mend;
%test();
Any help would be greatly appreciated.
Your nested %do loops are using the same index variable i
Try using distinct variables with semantic meaning to the problem at hand
%macro test;
…
%do iterationIndex = 1 %to 6;
…
%do referenceIndex = 1 %to &REFCOUNT;
…
%end;
%end;
…
%mend;
%test;

Analyze weather data stored in csv

I have some weather data stored in a csv file in the form of: „id, date, temperature, rainfall“, with id being the weather station and, obviously, date being the date of measurement. The file contains the data of 3 different stations over a period of 10 years.
What I'd like to do is analyze the data of each station and each year. For example: I'd like to calculate day-to-day differences in temperature [abs((n+1)-n)] for each station and each year.
I thought while-loops could be a possibility, with the loop calculating something as long as the id value is equal to the one in the next row.
But I’ve no idea how to do it.
Best regards
If you still need assistance, I would consider importing the .csv file data using "readtable". So long as only the first row are text, MATLAB will create a 'table' variable (this shouldn't be an issue for a .csv file). The individual columns can be accessed via "tablename.header" and can be reestablished as double data type (ex variable_1=tablename.header). You can then concatenate your dataset as you like. As for sorting by date and station id, I would advocate using "sortrows". For example, if the station id is the first column, sortrow(data,1) will sort "data" by the station id. sortrow(data, [1 2]) will sort "data" by the first column, then by the second column. From there, you can write an if statement to compare the station id's and perform the required calculations. I hope my brief answer is somewhat helpful.
A basic code structure would be:
path=['copy and paste file path here']; % show matlab where to look
data=readtable([path '\filename.csv'], 'ReadVariableNames',1); % read the file from csv format to table
variable1=data.header1 % general example of making double type variable from table
variable2=data.header2
variable3=data.header3
double_data=[variable1 variable2 variable3]; % concatenates the three columns together
sorted_data=sortrows(double_data, [1 2]); % sorts double_data by column 1 then column 2
It always helps to have actual data to work on and specifics as to what kind of output format is expected. Basically, ins and outs :) With the little info provided, I figured I would generate random data for you in the first section, and then calculate some stats in the second. I include the loop as an example since that's what you asked, but I highly recommend using vectorized calculations whenever available, such as the one done in summary stats.
%% example for weather stations
% generation of random data to correspond to what your csv file looks like
rng(1); % keeps the random seed for testing purposes
nbDates = 1000; % number of days of data
nbStations = 3; % number of weather stations
measureDates = repmat((now()-(nbDates-1):now())',nbStations,1); % nbDates days of data ending today
stationIds = kron((1:nbStations)',ones(nbDates,1)); % assuming 3 weather stations with IDs [1,2,3]
temp = rand(nbStations*nbDates,1)*70+30; % temperatures are in F and vary between 30 and 100 degrees
rain = max(rand(nbStations*nbDates,1)*40-20,0); % rain fall is 0 approximately half the time, and between 0mm and 20mm the rest of the time
csv = table(measureDates, stationIds, temp, rain);
clear measureDates stationIds temps rain;
% augment the original dataset as needed
years = year(csv.measureDates);
data = [csv,array2table(years)];
sorted = sortrows( data, {'stationIds', 'measureDates'}, {'ascend', 'ascend'} );
% example looping through your data
for i = 1 : size( sorted, 1 )
fprintf( 'Id=%d, year=%d, temp=%g, rain=%g', sorted.stationIds( i ), sorted.years( i ), sorted.temp( i ), sorted.rain( i ) );
if( i > 1 && sorted.stationIds( i )==sorted.stationIds( i-1 ) && sorted.years( i )==sorted.years( i-1 ) )
fprintf( ' => absolute difference with day before: %g', abs( sorted.temp( i ) - sorted.temp( i-1 ) ) );
end
fprintf( '\n' ); % new line
end
% depending on the statistics you wish to do, other more efficient ways of
% accessing summary stats might be accessible, for example:
grpstats( data ...
, {'stationIds','years'} ... % group by categories
, {'mean','min','max','meanci'} ... % statistics we want
, 'dataVars', {'temp','rain'} ... % variables on which to calculate stats
) % doesn't require data to be sorted or any looping
This produces one line printed for each row of data (and only calculates difference in temperature when there is no year or station change). It also produces some summary stats at the end, here's what I get:
stationIds years GroupCount mean_temp min_temp max_temp meanci_temp mean_rain min_rain max_rain meanci_rain
__________ _____ __________ _________ ________ ________ ________________ _________ ________ ________ ________________
1_2016 1 2016 82 63.13 30.008 99.22 58.543 67.717 6.1181 0 19.729 4.6284 7.6078
1_2017 1 2017 365 65.914 30.028 99.813 63.783 68.045 5.0075 0 19.933 4.3441 5.6708
1_2018 1 2018 365 65.322 30.218 99.773 63.275 67.369 4.7039 0 19.884 4.0615 5.3462
1_2019 1 2019 188 63.642 31.16 99.654 60.835 66.449 5.9186 0 19.864 4.9834 6.8538
2_2016 2 2016 82 65.821 31.078 98.144 61.179 70.463 4.7633 0 19.688 3.4369 6.0898
2_2017 2 2017 365 66.002 30.054 99.896 63.902 68.102 4.5902 0 19.902 3.9267 5.2537
2_2018 2 2018 365 66.524 30.072 99.852 64.359 68.69 4.9649 0 19.812 4.2967 5.6331
2_2019 2 2019 188 66.481 30.249 99.889 63.647 69.315 5.2711 0 19.811 4.3234 6.2189
3_2016 3 2016 82 61.996 32.067 98.802 57.831 66.161 4.5445 0 19.898 3.1523 5.9366
3_2017 3 2017 365 63.914 30.176 99.902 61.932 65.896 4.8879 0 19.934 4.246 5.5298
3_2018 3 2018 365 63.653 30.137 99.991 61.595 65.712 5.3728 0 19.909 4.6943 6.0514
3_2019 3 2019 188 64.201 30.078 99.8 61.319 67.082 5.3926 0 19.874 4.4541 6.3312

The counter counts strangly

My code describes a FSM to control a traffic light. There are four states, each with a different
duration.
Whenever the counter equals 1, the counter needs one more clock to change to the next value. For example, at state1, counter is programmed to count from 4 to 1. Every value should only take one clock to
change to the next, when it does, the state is changed to the next state. But when counter equals 1, it takes two clocks to change.
My program is as follows. The counter is implemented at the bottom of the always block:
module HW3(times,A,B,clk,rst,iHand,iChang,s1);
input clk,rst;
output reg [2:0]A,B;
wire oclk;//new freq
reg [2:0] count1,count2,count3,count4;//count times
reg [2:0]times;
reg temp;//control the switch
parameter [2:0]state1=3'd0,state2=3'd1,state3=3'd2,state4=3'd3;
always#(posedge clk or negedge rst )
begin
if(!rst)
begin
s1<=state1;
A<=3'b0;
B<=3'b0;
count1<=3'd4;
count2<=3'd2;
count3<=3'd3;
count4<=3'd2;
temp<=1'b1;
end
else
begin
if(temp==1)
begin
temp<=1'b0;
case(s1)
state1:
begin
times<=count1;
A<=3'b001;
B<=3'b100;
s1<=state2;
end
state2:
begin
times<=count2;
A<=3'b010;
B<=3'b100;
s1<=state3;
end
state3:
begin
times<=count3;
A<=3'b100;
B<=3'b001;
s1<=state4;
end
state4:
begin
times<=count4;
A<=3'b100;
B<=3'b010;
s1<=state1;
end
default:
begin
A<=3'b000;
B<=3'b000;
end
endcase
end
else
begin
if(times>1)
times<=times-1;
else if(times==1)
begin
temp<=1'b1;//can't count averagely
end
end
end
end
endmodule
Modify the code at the bottom of the always clock as:
if(times>2)
times<=times-1;
else if(times==2)
begin
times=times-1;
temp<=1'b1;//can't count averagely
end
Just let the times counts to 2 ,because if let it count to 1, the program will again enter the if
block in the next clock but doesnt change the value of times ,and make the value of times=1 unchanged
for one more clock

reducing running time for matlab for loops

my matlab code bellow takes a very long time to run. How could I compose it to take just seconds to run? I would be thankful if you could help me. parameters i, k_hat and alpha are constants.
%%%%%%%%%%%%%%%%%%%
p_desired=zeros(1,length(alpha));
p_ICI=zeros(1,length(alpha));
p_MUI=zeros(1,length(alpha));
tic
for ss=1:length(alpha)
uN=2*pi/N*Ts*abs(sin(alpha(ss)));
uM=2*pi/M*Ts*abs(sin(alpha(ss)));
for j=0:M/N-1
for k=0:N-1
for q=0:N-1
for qq=0:N-1
for q_hat=0:N-1
for qq_hat=0:N-1
for n=0:M-1
for m=-Ng:M-1
for n_hat=0:N-1
for m_hat=-Ng:N-1
if (n-m-n_hat+m_hat==0)
if (i==j && k==k_hat)
p_desired(ss)=p_desired(ss)+((sin(-alpha(ss))-1i*cos(-alpha(ss)))/N)^.5*exp(1i/2*k_hat^2*cot(-alpha(ss))*uN^2)*exp(1i/2*q^2*cot(-alpha(ss))*Ts^2)*exp(-1i*2*pi*q*k_hat/N)...
*((sin(-alpha(ss))+1i*cos(-alpha(ss)))/N)^.5*exp(-1i/2*k_hat^2*cot(-alpha(ss))*uN^2)*exp(-1i/2*q_hat^2*cot(-alpha(ss))*Ts^2)*exp(+1i*2*pi*q_hat*k_hat/N)...
* ((sin(alpha(ss))-1i*cos(alpha(ss)))/N)^.5*exp(1i/2*qq^2*cot(alpha(ss))*uN^2)*exp(1i/2*k_hat^2*cot(alpha(ss))*Ts^2)*exp(-1i*2*pi*qq*k_hat/N)...
*((sin(alpha(ss))+1i*cos(alpha(ss)))/N)^.5*exp(-1i/2*qq_hat^2*cot(alpha(ss))*uN^2)*exp(-1i/2*k_hat^2*cot(alpha(ss))*Ts^2)*exp(+1i*2*pi*qq_hat*k_hat/N)...
*((sin(alpha(ss))-1i*cos(alpha(ss)))/M)^.5*exp(1i/2*(q+N*(i-1))^2*cot(alpha(ss))*uM^2)*exp(1i/2*n^2*cot(alpha(ss))*Ts^2)*exp(-1i*2*pi*(q+N*(i-1))*n/M)...
*((sin(alpha(ss))+1i*cos(alpha(ss)))/M)^.5*exp(-1i/2*(q_hat+N*(i-1))^2*cot(alpha(ss))*uM^2)*exp(-1i/2*n_hat^2*cot(alpha(ss))*Ts^2)*exp(+1i*2*pi*(q_hat+N*(i-1))*n_hat/M)...
*((sin(-alpha(ss))-1i*cos(-alpha(ss)))/M)^.5*exp(1i/2*m^2*cot(-alpha(ss))*uM^2)*exp(1i/2*(qq+N*(i-1))^2*cot(-alpha(ss))*Ts^2)*exp(-1i*2*pi*m*(qq+N*(i-1))/M)...
*((sin(-alpha(ss))+1i*cos(-alpha(ss)))/M)^.5*exp(-1i/2*m_hat^2*cot(-alpha(ss))*uM^2)*exp(-1i/2*(qq_hat+N*(i-1))^2*cot(-alpha(ss))*Ts^2)*exp(+1i*2*pi*m_hat*(qq_hat+N*(i-1))/M)...
*exp(1i*2*pi/M*(n-n_hat)*e)*exp(-abs(n-m)/gamma)* besselj(0,2*pi*abs(n-n_hat)*fd);
elseif (i==j && k~=k_hat)
p_ICI(ss)=p_ICI(ss)+((sin(-alpha(ss))-1i*cos(-alpha(ss)))/N)^.5*exp(1i/2*k_hat^2*cot(-alpha(ss))*uN^2)*exp(1i/2*q^2*cot(-alpha(ss))*Ts^2)*exp(-1i*2*pi*q*k_hat/N)...
*((sin(-alpha(ss))+1i*cos(-alpha(ss)))/N)^.5*exp(-1i/2*k_hat^2*cot(-alpha(ss))*uN^2)*exp(-1i/2*q_hat^2*cot(-alpha(ss))*Ts^2)*exp(+1i*2*pi*q_hat*k_hat/N)...
* ((sin(alpha(ss))-1i*cos(alpha(ss)))/N)^.5*exp(1i/2*qq^2*cot(alpha(ss))*uN^2)*exp(1i/2*k^2*cot(alpha(ss))*Ts^2)*exp(-1i*2*pi*qq*k/N)...
*((sin(alpha(ss))+1i*cos(alpha(ss)))/N)^.5*exp(-1i/2*qq_hat^2*cot(alpha(ss))*uN^2)*exp(-1i/2*k^2*cot(alpha(ss))*Ts^2)*exp(+1i*2*pi*qq_hat*k/N)...
*((sin(alpha(ss))-1i*cos(alpha(ss)))/M)^.5*exp(1i/2*(q+N*(i-1))^2*cot(alpha(ss))*uM^2)*exp(1i/2*n^2*cot(alpha(ss))*Ts^2)*exp(-1i*2*pi*(q+N*(i-1))*n/M)...
*((sin(alpha(ss))+1i*cos(alpha(ss)))/M)^.5*exp(-1i/2*(q_hat+N*(i-1))^2*cot(alpha(ss))*uM^2)*exp(-1i/2*n_hat^2*cot(alpha(ss))*Ts^2)*exp(+1i*2*pi*(q_hat+N*(i-1))*n_hat/M)...
*((sin(-alpha(ss))-1i*cos(-alpha(ss)))/M)^.5*exp(1i/2*m^2*cot(-alpha(ss))*uM^2)*exp(1i/2*(qq+N*(i-1))^2*cot(-alpha(ss))*Ts^2)*exp(-1i*2*pi*m*(qq+N*(i-1))/M)...
*((sin(-alpha(ss))+1i*cos(-alpha(ss)))/M)^.5*exp(-1i/2*m_hat^2*cot(-alpha(ss))*uM^2)*exp(-1i/2*(qq_hat+N*(i-1))^2*cot(-alpha(ss))*Ts^2)*exp(+1i*2*pi*m_hat*(qq_hat+N*(i-1))/M)...
*exp(1i*2*pi/M*(n-n_hat)*e)*exp(-abs(n-m)/gamma) *besselj(0,2*pi*abs(n-n_hat)*fd);
elseif (i~=j)
p_MUI(ss)=p_MUI(ss)+((sin(-alpha(ss))-1i*cos(-alpha(ss)))/N)^.5*exp(1i/2*k_hat^2*cot(-alpha(ss))*uN^2)*exp(1i/2*q^2*cot(-alpha(ss))*Ts^2)*exp(-1i*2*pi*q*k_hat/N)...
*((sin(-alpha(ss))+1i*cos(-alpha(ss)))/N)^.5*exp(-1i/2*k_hat^2*cot(-alpha(ss))*uN^2)*exp(-1i/2*q_hat^2*cot(-alpha(ss))*Ts^2)*exp(+1i*2*pi*q_hat*k_hat/N)...
* ((sin(alpha(ss))-1i*cos(alpha(ss)))/N)^.5*exp(1i/2*qq^2*cot(alpha(ss))*uN^2)*exp(1i/2*k^2*cot(alpha(ss))*Ts^2)*exp(-1i*2*pi*qq*k/N)...
*((sin(alpha(ss))+1i*cos(alpha(ss)))/N)^.5*exp(-1i/2*qq_hat^2*cot(alpha(ss))*uN^2)*exp(-1i/2*k^2*cot(alpha(ss))*Ts^2)*exp(+1i*2*pi*qq_hat*k/N)...
*((sin(alpha(ss))-1i*cos(alpha(ss)))/M)^.5*exp(1i/2*(q+N*(i-1))^2*cot(alpha(ss))*uM^2)*exp(1i/2*n^2*cot(alpha(ss))*Ts^2)*exp(-1i*2*pi*(q+N*(i-1))*n/M)...
*((sin(alpha(ss))+1i*cos(alpha(ss)))/M)^.5*exp(-1i/2*(q_hat+N*(i-1))^2*cot(alpha(ss))*uM^2)*exp(-1i/2*n_hat^2*cot(alpha(ss))*Ts^2)*exp(+1i*2*pi*(q_hat+N*(i-1))*n_hat/M)...
*((sin(-alpha(ss))-1i*cos(-alpha(ss)))/M)^.5*exp(1i/2*m^2*cot(-alpha(ss))*uM^2)*exp(1i/2*(qq+N*(j-1))^2*cot(-alpha(ss))*Ts^2)*exp(-1i*2*pi*m*(qq+N*(j-1))/M)...
*((sin(-alpha(ss))+1i*cos(-alpha(ss)))/M)^.5*exp(-1i/2*m_hat^2*cot(-alpha(ss))*uM^2)*exp(-1i/2*(qq_hat+N*(j-1))^2*cot(-alpha(ss))*Ts^2)*exp(+1i*2*pi*m_hat*(qq_hat+N*(j-1))/M)...
*exp(1i*2*pi/M*(n-n_hat)*e)*exp(-abs(n-m)/gamma)* besselj(0,2*pi*abs(n-n_hat)*fd);
end
else
p_desired(ss)=p_desired(ss);
p_ICI(ss)=p_ICI(ss);
p_MUI(ss)=p_MUI(ss);
end
end
end
end
end
end
end
end
end
end
end
end
toc
Remove "for ss=1:length(alpha)" and corresponding "end"
Convert all "alpha(ss)" to "alpha"
Convert all "p_desired(ss)" to "p_desired"
Convert all "p_ICI(ss)" to "p_ICI"
Convert all "p_MUI(ss)" to "p_MUI"
Convert all "*", "/" and "^" to ".*", './' and ".^" respectively.
Convert "elseif (i~=j)" to "else"
Remove below code:
else
p_desired(ss)=p_desired(ss);
p_ICI(ss)=p_ICI(ss);
p_MUI(ss)=p_MUI(ss);