2

Here is an example matrix A of Dimensions 6x3x8x5

Now if i use size(A), i get all the dimensions as a row vector

ans = [6 3 8 5]

If i want to get specific dimension(singular), i could use something like size(A,1) or size(A,3) etc..

What if i want specified set of dimensions for eg, size of 3rd and 4th dimensions or 2nd to nth dimension

What i want to do is something like size(A,3:4) or size(A,2:n) or size(A,[1 3 4])

But from the Doc, it appears that, input dimensions for size could only be a scalar. When i try to do this, i get this error:

>> size(A,[2 3])

Error using size
Dimension argument must be a positive integer scalar within indexing range.

I'm expecting the output to be

ans = [3 8]

FYI:

I'm trying to pass this as an input argument into another function like this:

out = someFunction(arg1,arg2,size(A,[2 3]))

What i'm currently doing is

[~,size2,size3,~] = size(A)

out = someFunction(arg1,arg2,[size2, size3])

I just wanted to use it directly without the first line. Obviously when we have only two dimensions, we use it directly just by doing size(A). why not in this case? Any alternative to make this a one-liner?

7
  • 1
    Short answer: yes, this is how it works. I don't see any problem with two lines myself. If you like, you might create your own modified size function that has the features you describe. Commented May 17, 2015 at 3:53
  • 1
    possible duplicate of How can I index a MATLAB array returned by a function without first assigning it to a local variable? Commented May 17, 2015 at 10:21
  • 1
    No, they are duplicates. size is a function which returns an array, and you want to index it, without first assigning it to a local variable. You just gave a name to the "function" - the rest is equal :) Commented May 17, 2015 at 10:42
  • 1
    I understand, but this is no option using size, which brings the duplicate question to the only possible solution. Just let the community decide, if it's duplicate or not :) You don't need to accept the duplicate suggestion Commented May 17, 2015 at 10:50
  • 1
    I shall wait then :) Also, (adding to my point). If MathWorks implements vector input for size function in any future release, these 2 questions will be completely different. Just saying ;) Commented May 17, 2015 at 11:27

2 Answers 2

4

That's just the way size is written.

If you wanted a one-liner, you can use subsref to index the one-output form of size:

out = someFunction(arg1,arg2,...
              subsref(size(A),struct('type','()','subs',{{[2,3]}})));

And if you're going to be doing this a lot, add a function somewhere on the Matlab path or make an line one:

sizes = @(A,dims) subsref(size(A),struct('type','()','subs',{{dims}}));
out   = someFunction(arg1,arg2,sizes(A,[2,3]));

You can also create sizes without a direct call to subsref by a little indirection with function handles:

getSizes = @(d,s) d(s);
sizes    = @(A,s) getSizes(size(A),s);

which may be clearer and more maintainable.

Sign up to request clarification or add additional context in comments.

5 Comments

Works great :) +1 i still don't understand the subsref function, even after reading the doc. How does it work? Could you pls elaborate it?
@SanthanSalai Basically, subsref is the function called whenever you index an array. So if we have a matrix A and index it like a15 = A(1,5), Matlab's interpreter/compiler (kind of) transforms that into a15 = subsref(A,struct('type','()','subs',{{[1,5]}})) in the background.
That makes some sense, so for size function you are doing that manually as MatLab doesn't do it by itself.. am i right?
@SanthanSalai Yes. The Matlab language does not support the syntax size(A)([2,3]) like some other languages do, so a call to subsref is the most direct way. However, I have added a simpler version that avoids a direct call to subsref if it is more to your liking. Also, if this answered your question, please accept it so the post is considered answered.
Alternate version is clever :) i like them both.. thanks for answering :)
3

Both Troy Haskin's answers and mine are borrowed from this question: How can I index a MATLAB array returned by a function without first assigning it to a local variable? I personally find the getfield approach appropriate for your case, where you just wrap getfield around your size function:

A = randn(1,2,3,4,5);   %// 5D double

out = getfield(size(A),{[2 3]})

out =

     2     3

Using subsref is probably the better approach as more direct and faster, but it could make your code less readable, as it is very specific hack.

2 Comments

yeah.. getfield is more readable. Also thanks for linking the original question. :)
Nice. I'm feel silly not looking for this earlier now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.