필터 지우기
필터 지우기

Logical indexing 3D array to get a 2D slice

조회 수: 2 (최근 30일)
Yi-xiao Liu
Yi-xiao Liu 2021년 3월 12일
답변: Walter Roberson 2021년 3월 12일
I have a piece of code that calculates potential for 3D space. Now I want to extract the potential at the YOZ plane:
[X,Y,Z]=meshgrid(-500:spacing:500);
V= some calculations;
V0=V(X==0);
The problem is that V0 is a 1D vector instead of a 2D matrix. How do I correctly extract the slice?

채택된 답변

Walter Roberson
Walter Roberson 2021년 3월 12일
You cannot. When you use a single parameter logical vector to index an array, the result is always a vector.
In limited circumstances you can reshape() the vector.
I suggest something more like
marginals = -500:spacing:500;
[X,Y,Z] = meshgrid(marginals);
V= some calculations;
xtarget = 0;
[~, xidx] = min(abs(marginals - xtarget));
V0 = permute(V(xidx, :, :), [2 3 1]); %safer than squeeze()

추가 답변 (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