Compact MATLAB matrix indexing notation

后端 未结 3 1118
悲&欢浪女
悲&欢浪女 2020-11-29 10:39

I\'ve got an n-by-k sized matrix, containing k numbers per row. I want to use these k numbers as indexes into a k-dimensional matrix. Is there any compact way of doing so in

3条回答
  •  [愿得一人]
    2020-11-29 11:13

    Convert your sub-indices into linear indices in a hacky way

    ksz = size(kDimensionalMatrix);
    cksz = cumprod([ 1 ksz(1:end-1)] );
    lidx = ( indexmatrix - 1 ) * cksz' + 1; #'
    % lindx is now (n)x1 linear indices into kDimensionalMatrix, one index per row of indexmatrix
    % access all n values:
    selectedValues = kDimensionalMatrix( lindx );
    

    Cheers!

提交回复
热议问题