Select only some Index in matrix

Hi,
I have two variables:
whos var1 var2
Name Size
var1 331x666x31
var2 11x1
Where:
var2 = [ 3 5 6 7 8 9 10 11 12 15 16]
I only want to keep this numbers as index in the third dimension of var1. It's correct If I just do the following?
var3 = var1(:,:,var2);
Or a better approach would be use a for loop?
for i1 = 1:size(var2,1)
dummy = var2(i1);
var3(:,:,i1) = var1(:,:,dummy);
end
I'm concerned with optimization since I need to do this inside another for loop. Which is better or is there another better approach?
Thank in advance

 采纳的回答

Matt J
Matt J 2022-3-3
编辑:Matt J 2022-3-3
The former approach is definitely better. It uses half as much memory and should be much faster for large var2.

2 个评论

Thanks for your fast answer.
You're welcome, but please Accept-click the answer if your question is now resolved.

请先登录,再进行评论。

更多回答(0 个)

类别

在 帮助中心 和 File Exchange 中查找有关 Matrix Indexing 的更多信息

产品

版本

R2019b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by