How to append values to an array in for loop?

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

"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))
% ^^^^^^^^^^^^^^^^^^^^
Atanu
Atanu 2022년 5월 5일
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.

댓글을 달려면 로그인하십시오.

 채택된 답변

dpb
dpb 2022년 5월 5일

1 개 추천

>> 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

Thank you very much. Although 'disp' works but when I try to store it to a variable it shows me only 1 row.
load('direction_table.mat');
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);
if any(ix)
x = [direction_table{i,:} dx(ix)];
end
end
x =
17.0000 177.2270 -118.2320 -382.9450 352.1560
dpb
dpb 2022년 5월 7일
You didn't subscript x -- writing a variable on the LH side replaces the entire variable with the RHS each time.
Atanu
Atanu 2022년 5월 7일
What should I use for subscript if I don't know the height of resulting table beforehand?
Well, the maximum number of rows you can have would be height(direction_table)/10 since that's the index expression for your loop...since you may have some data subsets that contain no matches, there may not be that many "real" answers, but it's the most you could possibly have.
So, allocate for that and then you can remove rows with nothing in them when done --
This is also a case where you would use a second indexing variable inside the if...end clause and only increment it there...
H=height(direction_table)-10;
out=nan(ceil(H/10),5);
j=0;
for i=1: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)
j=j+1;
out(j,:)=[direction_table{i,:} dx(ix)];
end
end
out=out(isfinite(out(:,1)),;);
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), :};

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품

릴리스

R2022a

질문:

2022년 5월 5일

댓글:

2022년 5월 8일

Community Treasure Hunt

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

Start Hunting!

Translated by