2010-09-16 110 views
1

例如,我想两个数字范围结合起来是这样的:如何在MATLAB中将数字范围连接到数组中?

1 2 3 4 5 11 12 13 14 15 16 17 18 19 20 

所以,我想:

a = 1:5,11:20 

,但没有奏效。

我也想以非硬编码的方式做到这一点,以便缺少的5个元素可以在任何索引处开始。

回答

5

对于你的榜样,你需要使用square brackets来连接你的两个行向量:

a = [1:5 11:20]; 

还是要使它不那么硬编码:

startIndex = 6; %# The starting index of the 5 elements to remove 
a = [1:startIndex-1 startIndex+5:20]; 

您可能还需要检查这些相关功能:HORZCAT,VERTCAT,CAT

还有其他一些方法可以做到这一点。首先,你可以首先将整个向量,则索引你不想和删除它们的元素(如将它们设置为空载体[]):

a = 1:20;  %# The entire vector 
a(6:10) = []; %# Remove the elements in indices 6 through 10 

你也可以使用set operations要做到这一点,如功能SETDIFF

a = setdiff(1:20,6:10); %# Get the values from 1 to 20 not including 6 to 10 
+0

感谢您的额外信息!您刚刚创建了一些新的S/O用户。 – 2010-09-16 17:44:41

相关问题