[英]How to solve index exceeds matrix dimensions - MATLAB
This system is to generate the distance using two points of coordinates. 该系统将使用两个坐标点生成距离。 Sheet 1 display the coordinate on each waypoint, Sheet 2 display the connection of the waypoints. 图纸1显示每个航路点的坐标,图纸2显示航路点的连接。
Hence I'm require to generate the distance out but I keep encounter this problem; 因此,我需要生成距离,但是我一直遇到这个问题。 "??? Index exceeds matrix dimensions." “ ???索引超出了矩阵尺寸。” How can I solve this problem. 我怎么解决这个问题。
Below are my coding and the data stored in excel. 下面是我的编码和存储在excel中的数据。
Coding 编码
M = xlsread('Region11.xlsx',1);
N = xlsread('Region11.xlsx',2);
Start = N(:,1);
End = N(:,2);
distance = distance([M(Start,3),M(Start,4)],[M(End,3),M(End,4)]);
a= deg2km(distance);
xlswrite('Region11.xlsx',a, 2,'C2');
Region11 - sheet 1 Region11-第1张
No. Waypoints Latitude Longitude
1 11001 1.37983 103.84421
2 11002 1.3808 103.84406
3 11003 1.38186 103.84375
4 11004 1.38376 103.84301
5 11005 1.3838 103.84293
6 11006 1.38361 103.84153
7 11007 1.38354 103.84118
8 11008 1.38342 103.84088
9 11009 1.38308 103.84022
10 11010 1.38301 103.8402
11 11011 1.37973 103.84191
12 11012 1.37964 103.84201
13 11013 1.37963 103.84206
14 11014 1.37983 103.8441
15 11015 1.38003 103.84408
16 11016 1.38003 103.84408
17 11017 1.37998 103.84411
18 11018 1.37982 103.84403
19 11019 1.37984 103.84371
20 11020 1.37991 103.84373
21 11021 1.37994 103.8439
22 11022 1.38001 103.84328
23 11023 1.37993 103.84315
24 11024 1.37977 103.8431
25 11025 1.37981 103.84308
26 11026 1.37978 103.84269
27 11027 1.37972 103.84266
28 11028 1.37948 103.84253
29 11029 1.37955 103.84232
30 11030 1.37965 103.84197
31 11031 1.37981 103.84188
32 11032 1.37987 103.84196
33 11033 1.38004 103.84175
34 11034 1.3801 103.84187
35 11035 1.38018 103.8417
36 11036 1.38024 103.84179
37 11037 1.38008 103.84207
38 11038 1.38021 103.84201
39 11039 1.37997 103.84213
40 11040 1.38013 103.8429
41 11041 1.38021 103.84289
42 11042 1.38022 103.84289
43 11043 1.38028 103.84367
44 11044 1.38028 103.84367
45 11045 1.38026 103.84363
46 11046 1.38052 103.844
47 11047 1.38053 103.84398
48 11048 1.38059 103.84407
49 11049 1.38099 103.84394
50 11050 1.381 103.84391
51 11051 1.3811 103.84388
52 11052 1.38108 103.84393
53 11053 1.38122 103.84387
54 11054 1.38127 103.84392
55 11055 1.38127 103.84384
56 11056 1.3816 103.8437
57 11057 1.38164 103.84374
58 11058 1.38169 103.84381
59 11059 1.38158 103.84354
60 11060 1.38157 103.84349
61 11061 1.38135 103.84289
62 11062 1.38133 103.84288
63 11063 1.38125 103.84286
64 11064 1.38124 103.84285
65 11065 1.38083 103.84286
66 11066 1.38083 103.84286
67 11067 1.38099 103.84203
68 11068 1.381 103.84203
69 11069 1.3817 103.841
70 11070 1.3817 103.84099
71 11071 1.38166 103.84094
72 11072 1.38199 103.84076
73 11073 1.38199 103.84076
74 11074 1.3824 103.84053
75 11075 1.3824 103.84053
76 11076 1.38242 103.84053
77 11077 1.38242 103.84053
78 11078 1.38247 103.84046
79 11079 1.38292 103.84028
80 11080 1.38291 103.84043
81 11081 1.38291 103.84039
82 11082 1.38295 103.84072
83 11083 1.38291 103.8407
84 11084 1.38306 103.84051
85 11085 1.38323 103.84038
86 11086 1.38338 103.84067
87 11087 1.38329 103.84069
88 11088 1.38331 103.84091
89 11089 1.38347 103.84079
90 560621 1.383063 103.840593
91 560623 1.38268 103.840794
92 560625 1.382201 103.841111
93 560627 1.381864 103.84128
94 560633 1.381204 103.84217
95 560634 1.381582 103.843162
96 560636 1.381192 103.843284
97 560635 1.380788 103.842238
98 560637 1.38034 103.842361
99 560638 1.380486 103.843468
100 560639 1.379957 103.8424
101 560640 1.380024 103.843584
Region11 - sheet 2 Region11-表格2
Start End Distance
11001 11002
11002 11003
11003 11004
11004 11005
11005 11006
11006 11007
11007 11008
11008 11009
11009 11010
11010 11011
11011 11012
11012 11013
11013 11014
11014 11001
11001 11015
11015 11048
11048 11002
11002 11053
11053 11058
11058 11003
11008 11089
11089 11086
11086 11085
11085 11009
11010 11079
11079 11078
11078 11074
11074 11072
11072 11071
11072 11035
11035 11033
11031 11030
11030 11011
11030 11012
11013 11029
11029 11027
11027 11024
11024 11019
11019 11018
11018 11014
11018 11017
11017 11016
11016 11015
11017 11021
11019 11020
11022 11023
11023 11024
11023 11025
11025 11040
11022 11042
11042 11041
11041 11040
11026 11027
11028 11029
11031 11032
11033 11034
11035 11036
11035 11037
11037 11038
11038 11039
11041 11065
11065 11066
11065 11063
11063 11064
11063 11061
11061 11062
11061 11060
11060 11059
11068 11067
11057 11056
11056 11058
11056 11054
11055 11053
11054 11049
11049 11050
11049 11051
11051 11052
11052 11046
11046 11047
11046 11048
11069 11070
11071 11070
11070 11072
11072 11073
11072 11074
11074 11075
11075 11076
11076 11077
11076 11078
11078 11081
11080 11081
11079 11081
11085 11084
11086 11087
11089 11088
11082 11083
560621 11087
11084 560621
11080 560621
11082 560621
11083 560623
560623 11077
560625 11073
560627 11069
560633 11068
560633 11062
560634 11059
560634 11057
560636 11064
560636 11050
560636 11055
560635 11067
560635 11066
560637 11038
560637 11036
560637 11040
560638 11042
560638 11043
560638 11047
560639 11039
560639 11032
560639 11026
560639 11025
560640 11022
560640 11045
560640 11020
560640 11021
560640 560640
Your mistake that you refer to the row numbers of the waypoint matrix with the ids of the waypoints. 您错误引用了航点矩阵的行号和航点ID。 What you really want to do is to find the corresponding row in the waypoints columns that matches to the waypoint id in start or end, respectively. 您真正想要做的是在航点列中找到分别与开始或结束中的航点ID匹配的对应行。
You can do this using the ismember
-function: 您可以使用ismember
来做到这一点:
[~, start] = ismember(N(:,1), M(:, 2));
[~, to] = ismember(N(:,2), M(:, 2));
distance = distance(M(start,3:4), M(to,3:4));
Note that I also changed your variable name End
to to
, as end
is a special word in MATLAB that I would suggest not to use in variable names. 请注意,我还将变量名End
更改to
,因为end
是MATLAB中的一个特殊词,建议不要在变量名中使用。 Just differentiating names by their case is potentially faulty. 仅通过大小写区分名称可能存在错误。
Finally, you can write your selection in the distance function using the :
-operator in a more compact way, as I have done above. 最后,可以像上面我所做的那样,以更紧凑的方式使用:
-operator在距离函数中编写选择。
I feel the need to give a few advices on usual bug tracking strategies. 我觉得有必要针对常见的错误跟踪策略提供一些建议。 Especially in vector based languages such as MATLAB you are able to write quite complex statements in one line. 尤其是在基于矢量的语言(例如MATLAB)中,您能够在一行中编写非常复杂的语句。 If this line contains an error, you will have to find a way to tackle that problem down to its root before being able to solve it. 如果此行包含错误,您将必须找到一种方法来从根本上解决该问题,然后才能解决它。
What I was doing with you in the comments was to try to do exactly that process. 我在评论中与您一起做的是尝试完全执行该过程。 Reduce the data step by step and see where the error is happening. 逐步减少数据,并查看错误发生的位置。 Usually, that is something you should be able to do on your own. 通常,这是您应该能够自己完成的事情。
So, let's get into an example: 因此,让我们来看一个例子:
You notice there is a problem in the statement 您发现语句中有问题
deg2km(distance)
There are only two things here that could go wrong. 这里只有两件事可能出错。 Either you give the wrong input or deg2km
is faulty. 输入错误或deg2km
错误。 As deg2km
is a function provided by MATLAB, it is highly unlikely (though not impossible) that the error is within that function. 由于deg2km
是MATLAB提供的函数,因此错误在该函数内的可能性很小(尽管不是不可能)。 Thus, we have to understand what the problem with your input is. 因此,我们必须了解您输入的问题是什么。
Now I see a few options that might cause the problem: 现在,我看到了一些可能导致此问题的选项:
distance
variable is completely wrong distance
变量格式的数据类型完全错误 distance
variable distance
变量的一些输入只有问题 To identify the correct one of those options you have to try them systematically. 要确定这些选项中的正确选项,您必须系统地尝试它们。 First try: 第一次尝试:
deg2km(0.000981522767956357)
That one works. 那一个有效。 Thus, we are not missing further arguments. 因此,我们不会错过进一步的争论。
deg2km([0.000981; 08.9421])
That one works as well. 那也行得通。 We now know that we may pass a column vector to deg2km
without further problems. 现在我们知道可以将列向量传递到deg2km
而不会出现其他问题。 Now there are only two things left: It is a specific entry in distance
that causes trouble or the format of distance
is not a column vector. 现在只剩下两件事了:它是distance
中的一个特定条目,这会引起麻烦,或者distance
的格式不是列向量。
Let's have a try about that: If I understood you correctly, we have distance(1) == 0.000981522767956357
. 让我们尝试一下:如果我对您的理解正确,我们就会得到distance(1) == 0.000981522767956357
。 As we know that this number is a valid input of deg2km
, we may try the following: 我们知道这个数字是deg2km
的有效输入,我们可以尝试以下操作:
deg2km(distance(1))
Does that work? 那样有用吗? If yes, we look further 如果是,我们会进一步
deg2km(distance(1:2))
Does that work? 那样有用吗? If no, we should check what the systematic difference between distance(1:2)
and deg2km([0.000981; 08.9421])
is - that is the thing that is likely to cause the error. 如果不是,我们应该检查distance(1:2)
和deg2km([0.000981; 08.9421])
之间的系统差异是什么-这很可能导致错误。 If it works, however, let's do 如果可行,那就开始吧
deg2km(distance(1:20))
What about this one? 这个如何? If that does not work, look into distance(3:20)
. 如果那不起作用,请查看distance(3:20)
。 Something looking strange? 看起来有些奇怪? You could also try to narrow the problem further down: 您还可以尝试进一步缩小问题的范围:
deg2km(distance(1:10))
If that works, we know the problem is located at an entry in distance(11:20)
. 如果可行,我们知道问题出在distance(11:20)
的条目处。 Do that as long as you need until you understood what is causing the problem. 做到这一点,只要你需要,直到你明白是什么原因造成的问题。 Then we should try to understand what causes that input. 然后,我们应该尝试了解导致该输入的原因。 After that, fixing the problem usually is only a matter of seconds. 之后,解决问题通常只需几秒钟。
That kind of strategy can be applied to almost all problems you might encounter. 这种策略可以应用于您可能遇到的几乎所有问题。 It would also have helped you to understand your first problem better. 这也将帮助您更好地理解第一个问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.