sub2ind

sub2ind該函式用於把數組或矩陣中元素的全下標轉化為單下標

函式簡介

函式功能:在FreeMat、Matlab中,該函式用於把數組中元素全下標轉化為單下標。

語法格式:

>> help sub2ind

sub2ind - Convert subscripts to linear indices

This MATLAB function returns the linear index equivalents to the row and column

subscripts rowSub and colSub for a matrix of size matrixSize.

linearInd = sub2ind(matrixSize, rowSub, colSub)

linearInd = sub2ind(arraySize, dim1Sub, dim2Sub, dim3Sub, ...)

其中matrixSize是矩陣的尺寸,arraySize是數組的尺寸,通常用size函式獲取。

rowSub和colSub是行與列

dimxSub代表第x維數

相關函式:ind2sub

把元素的單下標轉化為全下標,用法與sub2ind一致

程式示例

--> A = [10,11,12;20,21,22];

%以二維矩陣為例

--> sub2ind(size(A), 2, 2)

%MATLAB單下標的計數方式是先列後行,先從第一列第一行為1,第一列第二行為2

ans =4

--> sub2ind(size(A), 2, 1)

ans = 2

--> A(2)

ans = 20

--> A(4)

ans = 21

-->ndims([])

%ndims函式用來計算矩陣或數組的維度,注意與size的區別

ans = 2

相關詞條

熱門詞條

聯絡我們