How to append values to an array in for loop?

35 visualizzazioni (ultimi 30 giorni)
rotation_time_by_direction = [];
for i = 1:10:length(direction_table.rodent_RecordingTime)-10
direction_change = ones(1,10)';
for j = 1:10
direction_change(j,1) = abs(direction_table.direction(i,1)-direction_table.direction(i+j,1));
threshold_degree = 100;
if any(direction_change > threshold_degree)
rotation_time_by_direction = cat(1, rotation_time_by_direction, direction_table.rodent_RecordingTime(i));
end
end
end
I am trying append the rodent_RecordingTime values to rotation_time_by_direction array when rotation is more than 100 degrees over next 10 time steps. But I am not sure why the rodent_RecordingTime is appending to the array. I have attached the variables if you want to look at it. Any help is appreciated.
  2 Commenti
Stephen23
Stephen23 il 5 Mag 2022
"But I am not sure why the rodent_RecordingTime is appending to the array."
cat(1, rotation_time_by_direction, direction_table.rodent_RecordingTime(i))
% ^^^^^^^^^^^^^^^^^^^^
Struggling in MATLAB
Struggling in MATLAB il 5 Mag 2022
Sorry, I didn't get your comment. If you are asking what do I mean by "But I am not sure why the rodent_RecordingTime is appending to the array.", you can consider it redundant.

Accedi per commentare.

Risposta accettata

dpb
dpb il 5 Mag 2022
>> for i=1:10:height(direction_table)-10
dx=abs(direction_table.direction(i+1:i+9)-direction_table.direction(i));
ix=find(dx>100,1);
id=ix+i-1;
if any(ix), disp([direction_table{i,:} dx(ix)]),end
end
0 -5.1105 -465.77 -72.713 107.33
4 146.82 -398.22 -228.38 324.64
12 -126.12 -125.06 -378.62 102.2
13 -111.11 -120.79 -379.49 289.69
14 168.73 -118.5 -374.87 215.66
17 177.23 -118.23 -382.94 352.16
>>
  6 Commenti
Struggling in MATLAB
Struggling in MATLAB il 8 Mag 2022
Thank you everyone for your comments. I kept on trying with append method, and I think this gives me the result I wanted.
load('direction_table.mat');
rotation_time_by_direction = [];
for i = 1:10:height(direction_table)-10
direction_change = ones(1,10)';
for j = 1:10
direction_change(j,1) = abs(direction_table.direction(i,1)-direction_table.direction(i+j,1));
end
threshold_degree = 100;
if any(direction_change > threshold_degree)
rotation_time_by_direction = [rotation_time_by_direction; direction_table.rodent_RecordingTime(i)];
end
end
n_direction_table = direction_table{ismember(direction_table.rodent_RecordingTime,rotation_time_by_direction), :};

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Prodotti


Release

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by