Extracting rows form a matrix with indices.
34 ビュー (過去 30 日間)
古いコメントを表示
HI all,
i have a Matrix a=(24X30) and a vector b=(1,4,7,8,10,12) that represents the rows index. I would like to create a new Matrix C=(6X30). This matrix is composed of the 6 rows with indeces (1,4,7,8,10,12), the vector b.
How can I do that?
Thanks
0 件のコメント
採用された回答
dpb
2023 年 2 月 21 日
編集済み: dpb
2023 年 2 月 21 日
M=[[1:24].' randi(100,24,5)]; % a sample array
b=[1,4,7,8,10,12]; % the indexing vector
N=M(b,:) % the new array
illustrates vector indirect addressing does work.
NOTA BENE: the array M was created such that the first element is the row index value so can see which was selected by inspection to prove got what wanted...
2 件のコメント
dpb
2023 年 2 月 21 日
M=[[1:24].' randi(100,24,5)]; % a sample array
b=[1,4,7,8,10,12]; % the indexing vector
ix=randperm(numel(b)) % let's shuffle them around...
N=M(b(ix),:)
illustrates the addressing doesn't have to be in any specific order, either...
その他の回答 (1 件)
Adam Drake
2023 年 2 月 21 日
% Create Matrix A: 24 x 30
A = randi(100,[24 30]);
% Create Vector B: desired row indices
B = [1,4,7,8,10,12];
% Create Matrix C: desired rows from Matrix A, specified by B
C = A(B,:);
The desired rows are specified by B, all columns are specified by ':'.
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Matrices and Arrays についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!