Indexing 3-D matrix with 2-D matrix of indexes
6 ビュー (過去 30 日間)
古いコメントを表示
I have a 3-D matrix that represents data for XYZ. I would like to pull the values from data given a 2-D matrix reprenting the Z indices of the values I want to pull. I thought there would be an easy way to index this without using a loop since my data is rather large, but I am stumped.
Below I have an example of the output I want using a loop...
% Sample 3-D Matrix of Data
data = ones(3,3,3);
data(:,:,1) = 0.1*data(:,:,1);
data(:,:,2) = 0.2*data(:,:,2);
data(:,:,3) = 0.3*data(:,:,3);
% Sample 2-D matrix of Z indices
idx = randi(3,3,3);
% Code for Loop, would like to do this without loop
sz = size(idx);
output = nan(sz);
for i = 1:numel(idx)
[j,k] = ind2sub(sz,i);
output(i) = data(j,k,idx(i));
end
採用された回答
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Matrix Indexing についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!