2010-03-31 95 views
3

我需要調用使用linspace命令製作的矩陣的索引,並基於從示波器獲取的一些數據。因此,輸入的數據是雙倍的。不過,我真的不能撥打:類型轉換爲Octave/Matlab中的int

Time[V0Found] 

其中V0Found是像5.2然而,服用指數5足夠接近,所以我需要放棄小數。我用這個公式去掉小數點:

V0FoundDec = V0Found - mod(V0Found,1) 
Time[V0FoundDec] 

但是,即使這樣會減小十進制數,八度仍然會抱怨它。

那麼,我能做些什麼來將它轉換爲int?

回答

5

在MATLAB中,它應該是int8(x)int16(x)one of the other integer casts

但我很驚訝你需要爲索引做到這一點。嘗試

myarray(floor(indexlist)) 

myarray(round(indexlist)) 

其中myarray是您的陣列和indexlist是你的非整數指數的載體。


例如:

octave-3.2.3:8> v=rand(1,8)*10+1 
v = 

    3.1769 1.4397 8.7504 1.7424 6.9413 3.1663 8.4085 9.0179 

octave-3.2.3:9> a = (1:1:20).^2 
a = 

Columns 1 through 15: 

    1  4  9 16 25 36 49 64 81 100 121 144 169 196 225 

Columns 16 through 20: 

    256 289 324 361 400 

octave-3.2.3:10> a(floor(v)) 
ans = 

    9 1 64 1 36 9 64 81 
+0

你的想法確實有效,儘管它在腳本中不起作用,我猜測我必須對X有一個非常小的值。 – 2010-03-31 22:36:58

2

你可以使用小區,而不是功能,您的公式做四捨五入。

順便說一句,索引使用括號,而不是括號這樣做:

V0FoundDec = round(V0Found); 
Time(V0FoundDec) % not Time[V0FoundDec] 

在那是你的問題

0

在MATLAB的情況下,這樣做的正確方法是使用interp1命令插入。該命令的格式是

YOUT = interp1(XDATA,YDATA,辛,...) 或 YOUT = interp1(YDATA,辛,...) 其中外部數據,然後假定爲1:長度(YDATA)

如果要產生結果,你會從調用

V0FoundDec =時間(輪(V0found獲得))

你會說

V0FoundDec = interp1(時間,V0found, '最近')

,但你可以很容易地獲得線性插值(這是默認值)與

V0FoundDec = interp1(時間,V0found)

V0FoundDec = interp1(Time,V0found,'linear')

並且還可以推斷的限制之外(使用 'extrap' 或提供extrap值),其中

時間(輪(V0found))

將崩潰,如果輪(V0found)< 1或>長度(時間)