0

I was asking myself if there was a quicker way to do this in matlab :

Imagine we have a 10x2 vector V and we want to use the x dimension (number of lines, here 10) in a function or do whatever we want with it. The way I usually do it is this :

[x y]=size(V);
function(x)

But would it be possible to make it differently? Soemething like

function(size(V)(1))

Thanks for your help !

4
  • 2
    You can use size(v,1) for rows number, or size(v,2) for columns number Commented Jul 9, 2013 at 12:19
  • is your question specific to size function, or were you using size as an example? Commented Jul 9, 2013 at 12:27
  • @Shai Actually my question was about 'size' specifically. Though, if there is a method that works for every function that returns a vector, I'm very interested ! Commented Jul 9, 2013 at 12:33
  • @mwoua: for size specific you have the nice answer you got from Rody. For a more general solution you can see this question. Commented Jul 9, 2013 at 12:36

1 Answer 1

2

MATLAB's size can take a second input argument, indicating the dimension of which you would like to know the size. The output is scalar in that case:

x = size(V,1);
y = size(V,2);

See help size for more details.

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

4 Comments

Arg, hadn't thought about the help. Damn. Thanks to both of you !
@mwoua: to transcend beyond what you already know about a tool, all you need to do is read its manual :)
that's what I do for most of the functions, even when I (think) I know how to use them... But here I completely forgot to :)
@mwoua: as an aside, an extra argument specifying the dimension you're interested in works with a lot of builtin functions: any, all, mean, cumsum, sum, prod, min, max, to name a few.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.