フィルターのクリア

Extract 2d slice from a 3d matrix

36 ビュー (過去 30 日間)
Zehra Ese
Zehra Ese 2023 年 4 月 27 日
コメント済み: Stephen23 2023 年 4 月 27 日
I have a 3d-matrix A, x-y-z with a size of 260x260x258 containing dose values. I want to extract the values of a certain layer x-z (260x258) from this matrix, in order to plot it and see the dose distribution. Could u give me an advice how to do this? I need to extract several layers, but if I know how to do it for e specific layer I could expand it.

採用された回答

Benjamin Kraus
Benjamin Kraus 2023 年 4 月 27 日
編集済み: Benjamin Kraus 2023 年 4 月 27 日
If you have a 3-D matrix, and you want just a single X-Z slice, you probably need a mix of indexing and either shiftdim or permute or squeeze to shift your data into the first two dimensions. For example:
d = rand(260,260,258);
s = d(:,10,:); % Grab the 10th slice
size(s) % This will be [260 x 1 x 258]
ans = 1×3
260 1 258
s1 = squeeze(s); % "squeeze" out the middle singleton dimension
size(s1)
ans = 1×2
260 258
figure
imagesc(s1)
% Alternatively
s2 = permute(s,[1 3 2]); % swap the second and third dimensions, so the singleton dimension is last
size(s2)
ans = 1×2
260 258
figure
imagesc(s2)
% Alternatively
s3 = shiftdim(s,2); % Shift the dimensions twice, so the singleton dimension ends up at the end.
size(s3)
ans = 1×2
258 260
s4 = s3'; % Transpose to swap the two remaining dimensions.
size(s4)
ans = 1×2
260 258
figure
imagesc(s3)
  2 件のコメント
Zehra Ese
Zehra Ese 2023 年 4 月 27 日
Thanks a lot Benjamin, squezze works perfect!! And also many thanks for showing me other options.
Stephen23
Stephen23 2023 年 4 月 27 日
If you want robust code, use PERMUTE.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

製品


リリース

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by