Making a table from data using rec

38 visualizzazioni (ultimi 30 giorni)
Nathan
Nathan il 22 Ott 2025 alle 14:36
Modificato: Voss il 23 Ott 2025 alle 18:31
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.

Risposte (3)

Star Strider
Star Strider il 22 Ott 2025 alle 15:18
Modificato: Star Strider il 22 Ott 2025 alle 15:31
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")
T = 16708×2 table
Var1 Var2 ___________ _____ 03-Jan-1979 0.026 04-Jan-1979 0.021 05-Jan-1979 0.019 06-Jan-1979 0.019 07-Jan-1979 0.519 08-Jan-1979 0.214 09-Jan-1979 0.371 10-Jan-1979 0.483 11-Jan-1979 0.145 12-Jan-1979 0.06 13-Jan-1979 0.025 14-Jan-1979 0.027 15-Jan-1979 0.029 16-Jan-1979 0.069 17-Jan-1979 0.263 18-Jan-1979 0.108
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
R = 5377
rec_true = nnz(rec)
rec_true = 5377
rec_true_table = T(rec,:) % Table Of 'true' 'rec' Values
rec_true_table = 5377×2 table
Var1 Var2 ___________ _____ 07-Jan-1979 0.519 09-Jan-1979 0.371 10-Jan-1979 0.483 14-Jan-1979 0.027 15-Jan-1979 0.029 16-Jan-1979 0.069 17-Jan-1979 0.263 19-Jan-1979 0.356 20-Jan-1979 0.469 24-Jan-1979 0.092 25-Jan-1979 1.05 28-Jan-1979 0.25 29-Jan-1979 0.633 31-Jan-1979 0.191 01-Feb-1979 1.69 06-Feb-1979 0.323
rec_false_table = T(~rec,:) % Table Of 'false' 'rec' Values
rec_false_table = 11331×2 table
Var1 Var2 ___________ _____ 03-Jan-1979 0.026 04-Jan-1979 0.021 05-Jan-1979 0.019 06-Jan-1979 0.019 08-Jan-1979 0.214 11-Jan-1979 0.145 12-Jan-1979 0.06 13-Jan-1979 0.025 18-Jan-1979 0.108 21-Jan-1979 0.151 22-Jan-1979 0.085 23-Jan-1979 0.059 26-Jan-1979 0.248 27-Jan-1979 0.101 30-Jan-1979 0.179 02-Feb-1979 0.287
EDIT -- (22 Oct 2025 at 15:30)
Added 'rec_true_table' and 'rec_false_table'.
.

Mathieu NOE
Mathieu NOE il 22 Ott 2025 alle 15:06
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 Commenti
Mathieu NOE
Mathieu NOE il 22 Ott 2025 alle 16:16
yes , of course !!
Mathieu NOE
Mathieu NOE il 22 Ott 2025 alle 16:31
Modificato: Mathieu NOE il 22 Ott 2025 alle 16:50
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
Elapsed time is 0.027264 seconds.
% 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
Elapsed time is 0.002681 seconds.
% let's compare the first 20 values side by side
n = 20;
sidebyside = [rec(1:n)' rec2(1:n)]
sidebyside = 20×2
0 0 0 0 0 0 0 0 1 1 0 0 1 1 1 1 0 0 0 0 0 0 1 1 1 1 1 1 1 1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
% create you output table
true_table = T(rec2,:)
true_table = 5377×2 table
Var1 Var2 ___________ _____ 07-Jan-1979 0.519 09-Jan-1979 0.371 10-Jan-1979 0.483 14-Jan-1979 0.027 15-Jan-1979 0.029 16-Jan-1979 0.069 17-Jan-1979 0.263 19-Jan-1979 0.356 20-Jan-1979 0.469 24-Jan-1979 0.092 25-Jan-1979 1.05 28-Jan-1979 0.25 29-Jan-1979 0.633 31-Jan-1979 0.191 01-Feb-1979 1.69 06-Feb-1979 0.323

Accedi per commentare.


Voss
Voss il 23 Ott 2025 alle 18:29
Modificato: Voss il 23 Ott 2025 alle 18:31
T = readtable("GDF.xlsx");
q = T.(2); % second column of the data
% logical index of "rec" rows, according
% to the definition of "rec":
idx_rec = [false; q(2:end) > q(1:end-1)]; 
% table of only the "rec" rows from T:
T_rec = T(idx_rec,:);

Categorie

Scopri di più su Tables in Help Center e File Exchange

Prodotti


Release

R2025a

Community Treasure Hunt

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

Start Hunting!

Translated by