フィルターのクリア

wrong number of rows of cell array using length()-function

5 ビュー (過去 30 日間)
Nik Rocky
Nik Rocky 2020 年 12 月 1 日
コメント済み: Nik Rocky 2020 年 12 月 1 日
Hello dear community,
I get some error by using length in one Cell-Array on Traj_interpl{1,209}:
for traj_cnt = 1:length(Traj_interpl) %for every Traj-Cell
for t_row_cnt = 1:length(Traj_interpl{traj_cnt}) %for any row of Traj
if t_interpl(t_ref_cnt) == Traj_interpl{traj_cnt}(t_row_cnt,1) %if right row of Traj is chosen - HERE IS ERROR!!!
motorspeed_detected(traj_cnt) = Traj_interpl{traj_cnt}(t_row_cnt,2); %write detected value to array
break;
else
motorspeed_detected(traj_cnt) = 0;
end
end
end
Error: Index in position 1 exceeds array bounds (must not exceed 1).
My cell array is: Traj_interpl = 1×209 cell array
I found that:
Traj_interpl{1,59}
=
18.6300 185.2204
18.6400 185.5478
18.6500 185.8752
18.6600 186.0040
18.6700 185.6136
18.6800 185.2232
18.6900 184.8328
length(Traj_interpl{1,59}) = 7 % number of rows; works great
but
Traj_interpl{1,209}
=
65.9000 118.0449
length(Traj_interpl{1,209}) = 2 % number of rows + 1/ number of elements; Error
Why in the 59 cell the length give me a right amount of rows and in a 209 cell a wrong one? How can I avoid this? Thank you!

採用された回答

Timo Dietz
Timo Dietz 2020 年 12 月 1 日
編集済み: Timo Dietz 2020 年 12 月 1 日
You should use the size command to distinguish between row and col.
To be more precise:
length(X) returns the length of vector X. It is equivalent
to MAX(SIZE(X)) for non-empty arrays and 0 for empty ones.
  2 件のコメント
Rik
Rik 2020 年 12 月 1 日
You can also have a look at this highlight post.
Nik Rocky
Nik Rocky 2020 年 12 月 1 日
Thank you very much, I'm using [row,column]=size(Traj_interpl{1,209}) now!

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

その他の回答 (3 件)

Sriram Tadavarty
Sriram Tadavarty 2020 年 12 月 1 日
Hi Nik,
length returns the length of the largest array dimension in X. For vectors, the length is simply the number of elements. For arrays with more dimensions, the length is max(size(X)). The length of an empty array is zero.
In your case, {1,59} cell is a matrix with more number of rows, so length provided that value, whereas cell {1,209} has more number of columns, thus, length provided value 2.
To get the number of rows of a matrix, use can use size(X,1).
Hope this helps.
Regards,
Sriram
  2 件のコメント
Nik Rocky
Nik Rocky 2020 年 12 月 1 日
Hi Sriram, thank you, now I understand a principle! I'm happy now!
Nik Rocky
Nik Rocky 2020 年 12 月 1 日
Best regards, Nik

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


Image Analyst
Image Analyst 2020 年 12 月 1 日
length() always gives you the LONGEST dimension. Since you have more columns (2) than rows (1), it will return 2.
  1 件のコメント
Nik Rocky
Nik Rocky 2020 年 12 月 1 日
I understand now, thanks!

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


Nik Rocky
Nik Rocky 2020 年 12 月 1 日
If i want to build it in my for-loop, what is the nice way to do it?
Old part of code
for traj_cnt = 1:length(Traj_interpl)
for t_row_cnt = 1:length(Traj_interpl{traj_cnt})
if t_interpl(t_ref_cnt) == Traj_interpl{traj_cnt}(t_row_cnt,1)
new code:
for traj_cnt = 1:length(Traj_interpl) % here is allways 1 x multiple columns array, so length is ok
for t_row_cnt = 1:size(Traj_interpl{traj_cnt},1) %-new part
if t_interpl(t_ref_cnt) == Traj_interpl{traj_cnt}(t_row_cnt,1)
okay? Thank you!
  2 件のコメント
Rik
Rik 2020 年 12 月 1 日
I would discourage the use of length. Either use size, or height/width, or numel. That last one is very flexible, as it will simply let you loop over all elements without having to worry about the shape.
Nik Rocky
Nik Rocky 2020 年 12 月 1 日
Okay, I will change it! Thank you Rik!

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

カテゴリ

Help Center および File ExchangeCell Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by