I want to calculate velocity. How do I apply my code to all tables in all cells?

Hi,
I have a data set of distances (see "results_distances" attached) and want to calculate the velocity for each row in each table in each cell.
I have the following code:
results_velocity = cell(size(results_distances));
for i = 1:length(results_distances) % iterate over tables in cell array
if ~isempty(results_distances{i}) % check if table is not empty
table_data = results_distances{i};
v_table = table_data; % initialize output table with the same size as input table
% Calculate velocity for each numeric variable (column) in the table
for var = 1:width(table_data)
% Check if the variable is numeric
if isnumeric(table_data{:, var})
% Calculate velocity for each row
for row = 2:size(table_data, 1)
% Subtract previous value from current value and divide by time_interval
v_table{row, var} = (table_data{row, var} - table_data{row-1, var}) / time_interval;
end
end
end
results_velocity{i} = v_table; % store velocity table in output cell array
end
end
However, when I run my code, it seems to only apply the calculation to the first column of tables in the cell array (see "results_velocity"). What am I doing wrong?
Thanks!

 Accepted Answer

load('results_distances.mat')
results_distances is of size 3x12
results_distances
results_distances = 3x12 cell array
Columns 1 through 10 {2155x2 table} {2155x2 table} {2155x2 table} {2154x2 table} {2155x2 table} {2155x2 table} {2155x2 table} {2155x2 table} {2155x2 table} {2155x2 table} {2155x2 table} {2155x2 table} {2155x2 table} {2155x2 table} {2155x2 table} {2155x2 table} {2155x2 table} {2155x2 table} {2155x2 table} {2155x2 table} {2155x2 table} {2155x2 table} {2155x2 table} {2155x2 table} {2155x2 table} {2155x2 table} {2155x2 table} {2155x2 table} {2155x2 table} {2155x2 table} Columns 11 through 12 {2155x2 table} {2155x2 table} {2155x2 table} {2155x2 table} {2155x2 table} {2155x2 table}
so its length is 12. Recall that length() returns the size of the longest dimension.
length(results_distances)
ans = 12
Iterating from 1 to length(results_distances) is iterating over the first 12 elements of results_distances, which in this case is the first 4 columns (since results_distances has 3 rows).
A same-size example using a numeric matrix:
data = reshape(1:36,3,[])
data = 3x12
1 4 7 10 13 16 19 22 25 28 31 34 2 5 8 11 14 17 20 23 26 29 32 35 3 6 9 12 15 18 21 24 27 30 33 36
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
for i = 1:length(data)
data(i)
end
ans = 1
ans = 2
ans = 3
ans = 4
ans = 5
ans = 6
ans = 7
ans = 8
ans = 9
ans = 10
ans = 11
ans = 12
In order to iterate over all elements of results_distances, use numel instead of length.
time_interval = 1;
results_velocity = cell(size(results_distances));
for i = 1:numel(results_distances) % iterate over tables in cell array
if ~isempty(results_distances{i}) % check if table is not empty
table_data = results_distances{i};
v_table = table_data; % initialize output table with the same size as input table
% Calculate velocity for each numeric variable (column) in the table
for var = 1:width(table_data)
% Check if the variable is numeric
if isnumeric(table_data{:, var})
% Calculate velocity for each row
for row = 2:size(table_data, 1)
% Subtract previous value from current value and divide by time_interval
v_table{row, var} = (table_data{row, var} - table_data{row-1, var}) / time_interval;
end
end
end
results_velocity{i} = v_table; % store velocity table in output cell array
end
end
results_velocity
results_velocity = 3x12 cell array
Columns 1 through 10 {2155x2 table} {2155x2 table} {2155x2 table} {2154x2 table} {2155x2 table} {2155x2 table} {2155x2 table} {2155x2 table} {2155x2 table} {2155x2 table} {2155x2 table} {2155x2 table} {2155x2 table} {2155x2 table} {2155x2 table} {2155x2 table} {2155x2 table} {2155x2 table} {2155x2 table} {2155x2 table} {2155x2 table} {2155x2 table} {2155x2 table} {2155x2 table} {2155x2 table} {2155x2 table} {2155x2 table} {2155x2 table} {2155x2 table} {2155x2 table} Columns 11 through 12 {2155x2 table} {2155x2 table} {2155x2 table} {2155x2 table} {2155x2 table} {2155x2 table}

5 Comments

A side note: because you're initializing v_table = table_data; and then updating rows 2 through end of v_table, when the code is complete, the first row of v_table still contains the first row of distances from table_data.
Hi @Voss, thanks a bunch. And yes, to your side note: that is because when calculating the velocity I need to subtract the prvious value from the current one which doesnt work for the first line I suppose. Is there any workaround you suggest? Deleting the first row from each table?
You're welcome!
Taking the difference between adjacent elements will produce an array of size one less than the original array, so, yeah, ending up with a table with one fewer row than the original table is what I would do.
You can use the diff function to do it. diff(_,1,1) is the first difference in the first dimension.
load('results_distances.mat')
time_interval = 1;
results_velocity = cell(size(results_distances));
for i = 1:numel(results_distances) % iterate over tables in cell array
if isempty(results_distances{i}) % check if table is not empty
continue % skip if table is empty
end
table_data = results_distances{i};
idx = vartype('numeric');
v = diff(table_data{:,idx},1,1) / time_interval;
results_velocity{i} = array2table(v); % store velocity table in output cell array
end
results_velocity
results_velocity = 3x12 cell array
Columns 1 through 10 {2154x2 table} {2154x2 table} {2154x2 table} {2153x2 table} {2154x2 table} {2154x2 table} {2154x2 table} {2154x2 table} {2154x2 table} {2154x2 table} {2154x2 table} {2154x2 table} {2154x2 table} {2154x2 table} {2154x2 table} {2154x2 table} {2154x2 table} {2154x2 table} {2154x2 table} {2154x2 table} {2154x2 table} {2154x2 table} {2154x2 table} {2154x2 table} {2154x2 table} {2154x2 table} {2154x2 table} {2154x2 table} {2154x2 table} {2154x2 table} Columns 11 through 12 {2154x2 table} {2154x2 table} {2154x2 table} {2154x2 table} {2154x2 table} {2154x2 table}
results_velocity{1}
ans = 2154x2 table
v1 v2 ___________ ___________ 7.5049e-06 9.9013e-07 1.5347e-05 7.4745e-06 -2.0066e-05 -1.2233e-05 -2.8578e-06 4.9597e-06 2.0392e-06 2.6013e-06 6.7307e-06 -2.8106e-06 8.1676e-06 4.4089e-06 4.2043e-06 -2.0911e-07 -7.4221e-06 2.1905e-05 -2.0846e-05 1.9876e-05 6.5665e-06 1.9515e-05 2.1943e-06 5.6662e-06 1.0346e-05 1.6792e-05 -4.9744e-06 -9.3965e-06 -1.2482e-06 -7.6492e-06 -4.7839e-06 -1.5141e-05
Wow this is perfect. Thank you again! Incredibly helpful and saves me so much time!

Sign in to comment.

More Answers (0)

Categories

Asked:

on 11 Jun 2024

Commented:

on 11 Jun 2024

Community Treasure Hunt

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

Start Hunting!