How to make a for loop run consecutively?

I have column vectors imported numerically that sometimes have NaN. This data corresponds to the position of an animal in a pool, where the NaN values at the beginning are not significant and need to be discarded, whereas there could be NaN values elsewhere (ie. after the first numerical value) that need to be addressed in a different manner. I'm not sure if this is the best way to approach this, but I envision using a while and for loop to evaluate the first row of the TF, if 1 delete row 1 in the associated column vectors, and then evaluate the new row 1 until row 1 is no longer a 1/NaN.
TF = isnan(X);
for m = 1
if TF(m) == 1
X(m) = [];
Y(m) = [];
Z(m) = [];
end
end
This is what I have so far. The only method I have found that works as intended is to copy and paste this for loop consecutively hundreds of times. However, I feel as if there must be a better way. Thanks for your help.

 採用された回答

Stephen23
Stephen23 2018 年 2 月 21 日
編集済み: Stephen23 2018 年 2 月 21 日

1 投票

Using a loop and removing one element at a time is very inefficient, because you force MATLAB to move the array in memory each time an element if removed: you will notice that the MATLAB editor underlines that code with a wavy orange line and a message advising against resizing this variable inside the loop. You should always pay attention to the warning and error messages shown by the editor.
You can remove the leading NaN's very simply and efficiently using find and some basic MATLAB indexing:
>> V = [NaN;NaN;54;12;7;2;NaN;100;354;NaN;0;3]
V =
NaN
NaN
54
12
7
2
NaN
100
354
NaN
0
3
>> V(find(~isnan(V),1,'first'):end)
ans =
54
12
7
2
NaN
100
354
NaN
0
3

その他の回答 (1 件)

KSSV
KSSV 2018 年 2 月 21 日

0 投票

YOu can remove the NaN's directly using:
TF(isnan(TF)) = [] ;
k = [NaN 2 3 NaN 5]
k(isnan(k)) = [] ;

1 件のコメント

Rameen Forghani
Rameen Forghani 2018 年 2 月 21 日
My data looks something like this: [NaN NaN NaN 3 5 2 NaN 1] where I only want to remove the first 3 NaN.

サインインしてコメントする。

カテゴリ

質問済み:

2018 年 2 月 21 日

編集済み:

2018 年 2 月 21 日

Community Treasure Hunt

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

Start Hunting!

Translated by