extract data from a 3D matrix using a window
4 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(1 个)
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
Qstore{k}
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Workspace Variables and MAT Files 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!