extract data from a 3D matrix using a window
4 views (last 30 days)
Show older comments
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 !!!
0 Comments
Answers (1)
Mathieu NOE
on 19 Mar 2025
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
Qstore{k}
0 Comments
See Also
Categories
Find more on Workspace Variables and MAT Files in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!