2010-09-16 108 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

相關問題