extract data from a 3D matrix using a window

4 ビュー (過去 30 日間)
Jingtao
Jingtao 2025 年 3 月 19 日
回答済み: Mathieu NOE 2025 年 3 月 19 日
I'd like to extract data along the 3rd dimension of a 3D matrix with the size of 2*2*10 using a window with a fixed size but a variable start indeces. But it doesn't work.
Script:
clc; clear;
a = 1:10; % 1*10 vector
J = reshape(repelem(a,2,2),2,2,length(a)); % expand a to 2*2*10 matrix
wsize = 5; % window size
startInd = [1,2;3,4]; % start index of the window
Q = J(:,:,startInd:startInd+wsize); % cannot extract accurately !!!

回答 (1 件)

Mathieu NOE
Mathieu NOE 2025 年 3 月 19 日
hello
there is one Q array per startInd value so either you use Q in a for loop without indexing (if you can tolerate to overwrite Q at each iteration, or store each result separately as shown below :
and also correct startInd dimensions as it should be a 1D vector
a = 1:10; % 1*10 vector
J = reshape(repelem(a,2,2),2,2,length(a)); % expand a to 2*2*10 matrix
wsize = 5; % window size
startInd = [1 2 3 4]; % start index of the window
for k = 1:numel(startInd)
disp(['-- Iteration index k = ' num2str(k) '--'])
Q = J(:,:,startInd(k):startInd(k)+wsize) % yes you can
% if you want to store each result separately
Qstore{k} = Q;
end
-- Iteration index k = 1--
Q =
Q(:,:,1) = 1 1 1 1 Q(:,:,2) = 2 2 2 2 Q(:,:,3) = 3 3 3 3 Q(:,:,4) = 4 4 4 4 Q(:,:,5) = 5 5 5 5 Q(:,:,6) = 6 6 6 6
-- Iteration index k = 2--
Q =
Q(:,:,1) = 2 2 2 2 Q(:,:,2) = 3 3 3 3 Q(:,:,3) = 4 4 4 4 Q(:,:,4) = 5 5 5 5 Q(:,:,5) = 6 6 6 6 Q(:,:,6) = 7 7 7 7
-- Iteration index k = 3--
Q =
Q(:,:,1) = 3 3 3 3 Q(:,:,2) = 4 4 4 4 Q(:,:,3) = 5 5 5 5 Q(:,:,4) = 6 6 6 6 Q(:,:,5) = 7 7 7 7 Q(:,:,6) = 8 8 8 8
-- Iteration index k = 4--
Q =
Q(:,:,1) = 4 4 4 4 Q(:,:,2) = 5 5 5 5 Q(:,:,3) = 6 6 6 6 Q(:,:,4) = 7 7 7 7 Q(:,:,5) = 8 8 8 8 Q(:,:,6) = 9 9 9 9
Qstore{k}
ans =
ans(:,:,1) = 4 4 4 4 ans(:,:,2) = 5 5 5 5 ans(:,:,3) = 6 6 6 6 ans(:,:,4) = 7 7 7 7 ans(:,:,5) = 8 8 8 8 ans(:,:,6) = 9 9 9 9

カテゴリ

Help Center および File ExchangeWorkspace Variables and MAT Files についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by