When indexing matrices in MATLAB, can I specify only the first or last n dimensions, and have all others dimensions "selected automatically"?
For example, I am writing a function which takes in an image, and displays it with imshow
, which can either display a 3-D color image (e.g 1024×768×3
) or a 2-D monochrome array (e.g 1024x768).
My function does not care about how many color channels the image has, imshow
will take care of that. All I want to do is pass parameters to select a single region:
imshow(frame(x1:x2, y1:y2, :))
What do I put in place of the last colon to say "include all the others dimensions"?
You can use comma-separated-list expansion together with the ':'
indexing.
Suppose your input is:
A = rand([7,4,2,3]);
To retrieve only first 2:
cln = {':', ':'};
A(cln{:})
To retrieve the last 3:
cln = {1, ':', ':', ':'};
A(cln{:})
Which can be generalized with:
sten = 2:3; % Which dims to retrieve
cln(1:ndims(A)) = {1};
cln(sten) = {':'};
A(cln{:})
Following from Oleg's answer, here is a function that will work if you are selecting from several of the first dimensions. If other dimensions are needed, I think you can see how to modify.
function [dat] = getblock2(dat, varargin)
%[dat] = getblock(dat, varargin) select subarray and retain all others
% unchanged
%dat2 = getblock(dat, [1,2], [3,5]) is equivalent to
% dat2 = dat(1:2, 3:5, :, :, :) etc.
%Peter Burns 4 June 2013
arg1(1:ndims(dat)) = {':,'};
v = cell2mat(varargin);
nv = length(v)/2;
v = reshape(v,2,nv)';
for ii=1:nv
arg1{ii} = [num2str(v(ii,1)),':',num2str(v(ii,2)),','];
end
arg2 = cell2mat(arg1);
arg2 = ['dat(',arg2(1:end-1),')'];
dat = eval(arg2);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With