简体   繁体   English

matlab cat()函数:索引超出矩阵维度

[英]Matlab cat() function: index exceeds matrix dimensions

I am trying to concatenate an array of numbers from 1->(a-1) + (a+1)->n. 我试图连接1 - >(a-1)+(a + 1) - > n的数字数组。

I was using the cat function 我正在使用cat功能

cat(2, 1:a-1, a+1:n)

but I am getting the error 但是我收到了错误

Index exceeds matrix dimensions.

Unless I am completely mistaken, I am just trying to concatenate two matrices of numbers so I'm not quite sure why I'm getting this error. 除非我完全弄错了,否则我只是想连接两个数字矩阵,所以我不太清楚为什么我会收到这个错误。

I'm trying to accomplish this: 我正在努力实现这个目标:

>> a = 3;
>> n = 10;
>> cat(2, 1:a-1, a+1:n)
ans = 
    [1,2,4,5,6,7,8,9,10]

Is this the wrong way to do it? 这是错误的方法吗? Any idea why this error is coming up? 知道为什么会出现这个错误吗?

Do you have a variable called cat in your workspace? 你的工作区中有一个名为cat的变量吗?

>> cat(2, 2:3, 4:6)                   # this works fine
ans =
     2     3     4     5     6
>> cat = 1:3;                         # introduce the variable 'cat'
>> cat(2, 2:3, 4:6)                   # now it breaks
??? Index exceeds matrix dimensions.

It looks like you have a variable named cat in the workspace. 看起来你在工作区中有一个名为cat的变量。 The clean way is, of course, to rename the variable: If you have a sufficiently recent version of Matlab (R2012x, I think), you can replace cat in the first line it gets assigned (select the variable to see the gray ticks to the right of the window, indicating where the variable occurs in the function), and use shift+enter to replace all occurrences. 当然,干净的方式是重命名变量:如果你有一个足够新版本的Matlab(R2012x,我认为),你可以在它被分配的第一行中替换cat (选择变量以查看灰色刻度到窗口右侧,指示变量在函数中出现的位置),并使用shift+enter替换所有出现的位置。 Or you can use the Find/Replace all function (make sure you only replace words, not substrings, though). 或者您可以使用查找/替换所有功能(确保您只替换单词,而不是替换子串)。

If you cannot replace the existing variable name, you can use square brackets for catenation along the first and/or second dimension: 如果无法替换现有变量名称,则可以在第一维和/或第二维上使用方括号进行连接:

cat(2,a,b)

is equivalent to 相当于

[a,b]

Just for completeness, the concatenation you're trying to accomplish can also be achieved like so: 为了完整起见,您尝试完成的连接也可以这样实现:

R = 1:n;
R = R(R ~= a)

I personally think this looks cleaner than 我个人认为这比看起来更干净

R = [1:a-1 a+1:n]

but that's personal; 但这是个人的; I always feel a little confusion towards something like 1:a-1>5 (is it ((1:a)-1)>5 or (1:(a-1))>5 or (1:a)-(1>5) or ...). 我总是对1:a-1>5 (是((1:a)-1)>5(1:(a-1))>5(1:a)-(1>5)或......)。 I just always have to think for a second, whereas I understand my solution instantly. 我只需要思考一下,而我立即理解我的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM