transposing a matrix of panel data

1 次查看(过去 30 天)
Mark Rhodes
Mark Rhodes 2019-4-5
回答: BhaTTa 2025-7-18
I have a panel data set for a single variable that is large in both dimensions and so I have had to read this into Matlab as a matrix.
Might I ask how I can transpose into a column vector but retaining the date for each observation?

回答(1 个)

BhaTTa
BhaTTa 2025-7-18
After reading the data into MATLAB workspace you can use repshape and repmat, i have shown with an example below:
% --- 1. Create Sample Data (Replace with your actual data) ---
numDates = 10;
numEntities = 5;
% Generate some random data for illustration
dataMatrix = rand(numDates, numEntities) * 100;
% Generate dates (e.g., daily dates starting from Jan 1, 2023)
startDate = datetime(2023, 1, 1);
datesVector = startDate + (0:numDates-1)'; % Column vector of dates
disp('Original dataMatrix:');
Original dataMatrix:
disp(dataMatrix);
60.7459 70.0640 32.9192 48.4185 37.7281 81.7824 66.4761 60.5502 69.3132 49.2070 3.6966 64.2181 57.4912 14.3682 12.0808 82.2825 80.7324 44.5260 5.6199 97.2214 57.9945 95.5892 68.5764 26.9516 5.7864 19.0789 42.9681 94.0305 96.0382 7.3151 22.8725 25.7669 29.0677 54.1106 21.3444 2.5826 53.5137 70.9805 13.0364 55.8054 92.5877 33.8100 88.4735 75.1340 46.6961 94.0604 95.8388 16.1250 97.6804 16.4945
disp('Original datesVector:');
Original datesVector:
disp(datesVector);
01-Jan-2023 02-Jan-2023 03-Jan-2023 04-Jan-2023 05-Jan-2023 06-Jan-2023 07-Jan-2023 08-Jan-2023 09-Jan-2023 10-Jan-2023
% --- 2. Reshape the dataMatrix into a single column vector ---
% The 'reshape' function operates column-wise.
% If your data is structured such that each column is a time series for an entity,
% then simply reshaping will stack these time series one after another.
reshapedData = reshape(dataMatrix, [], 1); % The '[]' tells reshape to automatically calculate the number of rows
% --- 3. Create a corresponding date vector for the reshaped data ---
% Each date needs to be repeated 'numEntities' times.
% You can use repmat for explicit repetition:
repeatedDates = repmat(datesVector, numEntities, 1);
% Alternatively, if you have recent MATLAB versions, implied broadcasting
% for element-wise operations with different dimensions might be available,
% but for explicit repetition for concatenation, repmat is clearer.
% Or, a more direct way if you know the order of reshaping:
% You want [date1_entity1, date1_entity2, ..., date1_entityN,
% date2_entity1, date2_entity2, ..., date2_entityN, ...]
% However, reshape works column-wise, so it would be:
% [date1_entity1, date2_entity1, ..., date_last_entity1,
% date1_entity2, date2_entity2, ..., date_last_entity2, ...]
% So, the 'datesVector' needs to be repeated 'numEntities' times,
% where each block of 'numDates' corresponds to one entity's time series.
% --- 4. Combine them into a table (recommended for panel data) ---
% A table is an excellent data structure for panel data in MATLAB,
% as it allows you to combine different data types (numeric, datetime)
% and label columns.
panelTable = table(repeatedDates, reshapedData, 'VariableNames', {'Date', 'Value'});
disp(' ');
disp('Reshaped Data (Column Vector):');
Reshaped Data (Column Vector):
disp(reshapedData);
60.7459 81.7824 3.6966 82.2825 57.9945 19.0789 22.8725 2.5826 92.5877 94.0604 70.0640 66.4761 64.2181 80.7324 95.5892 42.9681 25.7669 53.5137 33.8100 95.8388 32.9192 60.5502 57.4912 44.5260 68.5764 94.0305 29.0677 70.9805 88.4735 16.1250 48.4185 69.3132 14.3682 5.6199 26.9516 96.0382 54.1106 13.0364 75.1340 97.6804 37.7281 49.2070 12.0808 97.2214 5.7864 7.3151 21.3444 55.8054 46.6961 16.4945
disp(' ');
disp('Repeated Dates (Column Vector):');
Repeated Dates (Column Vector):
disp(repeatedDates);
01-Jan-2023 02-Jan-2023 03-Jan-2023 04-Jan-2023 05-Jan-2023 06-Jan-2023 07-Jan-2023 08-Jan-2023 09-Jan-2023 10-Jan-2023 01-Jan-2023 02-Jan-2023 03-Jan-2023 04-Jan-2023 05-Jan-2023 06-Jan-2023 07-Jan-2023 08-Jan-2023 09-Jan-2023 10-Jan-2023 01-Jan-2023 02-Jan-2023 03-Jan-2023 04-Jan-2023 05-Jan-2023 06-Jan-2023 07-Jan-2023 08-Jan-2023 09-Jan-2023 10-Jan-2023 01-Jan-2023 02-Jan-2023 03-Jan-2023 04-Jan-2023 05-Jan-2023 06-Jan-2023 07-Jan-2023 08-Jan-2023 09-Jan-2023 10-Jan-2023 01-Jan-2023 02-Jan-2023 03-Jan-2023 04-Jan-2023 05-Jan-2023 06-Jan-2023 07-Jan-2023 08-Jan-2023 09-Jan-2023 10-Jan-2023
disp(' ');
disp('Combined Panel Table:');
Combined Panel Table:
disp(panelTable);
Date Value ___________ ______ 01-Jan-2023 60.746 02-Jan-2023 81.782 03-Jan-2023 3.6966 04-Jan-2023 82.283 05-Jan-2023 57.995 06-Jan-2023 19.079 07-Jan-2023 22.872 08-Jan-2023 2.5826 09-Jan-2023 92.588 10-Jan-2023 94.06 01-Jan-2023 70.064 02-Jan-2023 66.476 03-Jan-2023 64.218 04-Jan-2023 80.732 05-Jan-2023 95.589 06-Jan-2023 42.968 07-Jan-2023 25.767 08-Jan-2023 53.514 09-Jan-2023 33.81 10-Jan-2023 95.839 01-Jan-2023 32.919 02-Jan-2023 60.55 03-Jan-2023 57.491 04-Jan-2023 44.526 05-Jan-2023 68.576 06-Jan-2023 94.031 07-Jan-2023 29.068 08-Jan-2023 70.981 09-Jan-2023 88.473 10-Jan-2023 16.125 01-Jan-2023 48.418 02-Jan-2023 69.313 03-Jan-2023 14.368 04-Jan-2023 5.6199 05-Jan-2023 26.952 06-Jan-2023 96.038 07-Jan-2023 54.111 08-Jan-2023 13.036 09-Jan-2023 75.134 10-Jan-2023 97.68 01-Jan-2023 37.728 02-Jan-2023 49.207 03-Jan-2023 12.081 04-Jan-2023 97.221 05-Jan-2023 5.7864 06-Jan-2023 7.3151 07-Jan-2023 21.344 08-Jan-2023 55.805 09-Jan-2023 46.696 10-Jan-2023 16.495
% You can verify the sizes
fprintf('\nSize of reshapedData: %dx%d\n', size(reshapedData,1), size(reshapedData,2));
Size of reshapedData: 50x1
fprintf('Size of repeatedDates: %dx%d\n', size(repeatedDates,1), size(repeatedDates,2));
Size of repeatedDates: 50x1
fprintf('Size of panelTable: %dx%d\n', size(panelTable,1), size(panelTable,2));
Size of panelTable: 50x2

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by