Indexing 3-D matrix with 2-D matrix of indexes
5 次查看(过去 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!