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 !

mwoua
  • 403
  • 5
  • 13
  • 2
    You can use `size(v,1)` for rows number, or `size(v,2)` for columns number – Adiel Jul 09 '13 at 12:19
  • is your question specific to `size` function, or were you using `size` as an example? – Shai Jul 09 '13 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 ! – mwoua Jul 09 '13 at 12:33
  • @mwoua: for `size` specific you have the nice answer you got from [Rody](http://stackoverflow.com/a/17548059/1714410). For a more general solution you can see [this question](http://stackoverflow.com/questions/3627107/how-can-i-index-a-matlab-array-returned-by-a-function-without-first-assigning-it). – Shai Jul 09 '13 at 12:36

1 Answers1

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.

Rody Oldenhuis
  • 37,726
  • 7
  • 50
  • 96
  • Arg, hadn't thought about the help. Damn. Thanks to both of you ! – mwoua Jul 09 '13 at 12:27
  • @mwoua: to transcend beyond what you already know about a tool, all you need to do is read its manual :) – Rody Oldenhuis Jul 09 '13 at 12:29
  • 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 Jul 09 '13 at 12:35
  • 3
    @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. – Rody Oldenhuis Jul 09 '13 at 12:49