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

1 visualización (últimos 30 días)
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!

Respuesta aceptada

Voss
Voss el 11 de Jun. de 2024
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 comentarios
lil brain
lil brain el 11 de Jun. de 2024
Wow this is perfect. Thank you again! Incredibly helpful and saves me so much time!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Tables en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by