How to fill-in a smaller table into a larger (empty) table with a sorted variable?
2 views (last 30 days)
Show older comments
I have extracted smaller tables from large data for further analysis. After extraction, the table needs to be sorted and has missing values. To address these two, I have made an empty table with the sorted variable (the channel 'variable'). I just want to shift the values of the extracted tables into the ref_table (empty table).
I have put a partial code for working below:
clc
clear all
close all
load("del.mat")
names = {T1,T2};
for i = 1:2
ref_table = DummyTable;
tab = names{i};
% 1. tab sorted in ascending 'channel' variable and filled-in the reftable
% 2. In second loop, the values of channel 5 are missing, and should be
% left as nan.
end
I can visually explain what I want to do in excel sheet screenshots below:
The red table is sortd first and then filled in the green table as below:
The missing values should be shown as nan, as shown in the next picture
Thank you.
0 Comments
Accepted Answer
Cris LaPierre
on 14 Oct 2024
Joining doesn't appear to selectively replace values in one table. With the assumption that all channels only appear once in the small tables, here's an approach that doesn't use loops.
load del.mat
[~,r] = ismember(T1.channel,DummyTable.channel)
T1new = DummyTable;
T1new(r,:) = T1
[~,r] = ismember(T2.channel,DummyTable.channel)
T2new = DummyTable;
T2new(r,:) = T2
More Answers (1)
Rahul
on 15 Oct 2024
I believe you are trying to take extracted tables (T1, T2) and transfer their values into a reference table (ref_table) sorted by the 'channel' variable, while handling missing values (channels that are missing should remain NaN), here's how you can modify your code:
- Sort the extracted table (tab) by the 'channel' variable.
- Match the rows of the sorted table (tab) with the 'channel' values of the reference table (ref_table).
- Transfer the values from the extracted table into the correct positions in the reference table.
clc
clear all
close all
% Load the data (del.mat contains T1, T2, and DummyTable)
load("del.mat")
% Names of the extracted tables
names = {T1, T2};
for i = 1:2
% Reference empty table (DummyTable)
ref_table = DummyTable;
% Current extracted table (T1 or T2)
tab = names{i};
% Sort the extracted table by the 'channel' variable in ascending order
tab = sortrows(tab, 'channel');
% Now, match 'channel' values of 'tab' with 'ref_table'
for j = 1:height(tab)
% Find the index of the current 'channel' in the ref_table
channel_value = tab.channel(j);
% Find the row in the ref_table where the 'channel' matches
idx = find(ref_table.channel == channel_value);
% If a matching channel is found, transfer the data
if ~isempty(idx)
% Transfer the corresponding data from tab to ref_table
ref_table(idx, :) = tab(j, :);
end
end
% Display the updated ref_table for the current table
disp(['Updated ref_table for T', num2str(i)]);
disp(ref_table);
end
For more information regarding the functions used in the given code snippet, refer to the folllowing documentation links:
- Sort rows of matrix or table using "sortrows" function: https://www.mathworks.com/help/matlab/ref/double.sortrows.html
Best!
2 Comments
Rahul
on 15 Oct 2024
Moved: Cris LaPierre
on 15 Oct 2024
I'm glad you found it useful, thanks @Muha.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!