Making a table from data using rec
25 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
I am trying to use this code to create a table of when the data is in recession (when the value of q(x)>q(x-1)). However I do not know how to use the rec command.
T= readtable ("GDF.xlsx");
q=T(:,2);%The second column of the data is numerical, the first column is just dates
R=0;
for x=T
if q(x)>q(x-1) %Defining what a recession counts as
R=R+1;
rec(x)=1 %If it is a recession it =1, if not it will =0
else
rec(x)=0;
end
end
Error message that appears when I run this code:
Error using () (line 133)
Subscripting into a table using one subscript (as in t(i))
is not supported. Specify a row subscript and a variable
subscript, as in t(rows,vars). To select variables, use
t(:,i) or for one variable t.(i). To select rows, use
t(i,:).
Error in
Week4Q2 (line 8)
if q(x)>q(x-1)
Any idea on how to change the code so it produces a table of when the data is in recession? Help would be appreciated.
0 Kommentare
Antworten (2)
Star Strider
vor etwa 8 Stunden
Bearbeitet: Star Strider
vor etwa 8 Stunden
Apparently, 'rec' is not a funciton. Here it is simply a vector.
I am not certain what 'R' is doing other than keeping a count. You can do that easily enough with the nnz funciton after the loop completes.
What sort of table do you want your code to produce? The 'rec' vector is a logical vector, so you can use it to produce a table of values where only 'rec' is true or conversely when it is false.
T= readtable ("GDF.xlsx")
rec = false(size(T.Var1)); % Preallocate
q=T{:,2};%The second column of the data is numerical, the first column is just dates
R=0;
for x=2:numel(rec)
if q(x)>q(x-1) %Defining what a recession counts as
R=R+1;
rec(x)=1; %If it is a recession it =1, if not it will =0
else
rec(x)=0;
end
end
R
rec_true = nnz(rec)
rec_true_table = T(rec,:) % Table Of 'true' 'rec' Values
rec_false_table = T(~rec,:) % Table Of 'false' 'rec' Values
EDIT -- (22 Oct 2025 at 15:30)
Added 'rec_true_table' and 'rec_false_table'.
.
0 Kommentare
Mathieu NOE
vor etwa 8 Stunden
hello
here you are
what has changed
- q must be converted from table to array - use table2array
- for loop index needed a fix : now it's : for x=2:numel(q) - this also implies that we must declare the first value of rec ( by default) is either 0 or 1 - there is no previous q value to test the recession
the updated code result with your supplied excel file gives R = 5377 recessions
T= readtable ("GDF.xlsx");
q=table2array(T(:,2));%The second column of the data is numerical, the first column is just dates
R=0;
rec(1) = 0;
for x=2:numel(q)
if q(x)>q(x-1) %Defining what a recession counts as
R=R+1;
rec(x)=1; %If it is a recession it =1, if not it will =0
else
rec(x)=0;
end
end
3 Kommentare
Mathieu NOE
vor etwa 7 Stunden
Bearbeitet: Mathieu NOE
vor etwa 6 Stunden
you can also use diff function to check the recession - instead of the for loop
see second alternative hereafter which is 10 times faster and more concise
T= readtable ("GDF.xlsx");
q=T{:,2};%The second column of the data is numerical, the first column is just dates
% your method
tic
R=0;
rec(1) = 0;
for x=2:numel(q)
if q(x)>q(x-1) %Defining what a recession counts as
R=R+1;
rec(x)=1; %If it is a recession it =1, if not it will =0
else
rec(x)=0;
end
end
toc
% another method
tic
rec2 = diff(q)>0; % logical array
% rec2 will be identical to rec but with one sample shift du to diff
% operation
% here we add a logical 0 (false) at the start (which is equivalent to the
% initilaization rec(1) = 0 as we did above in the first method);
rec2 = [false;rec2]; % add a logical 0 (false) at the start
toc
% let's compare the first 20 values side by side
n = 20;
sidebyside = [rec(1:n)' rec2(1:n)]
% create you output table
true_table = T(rec2,:)
Siehe auch
Kategorien
Mehr zu Tables finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!