Select only some Index in matrix

조회 수: 2 (최근 30일)
user20912
user20912 2022년 3월 3일
댓글: Matt J 2022년 3월 3일
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
user20912
user20912 2022년 3월 3일
Thanks for your fast answer.
Matt J
Matt J 2022년 3월 3일
You're welcome, but please Accept-click the answer if your question is now resolved.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile 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