8
votes

Given a vector A defined in Matlab by:

A =  [ 0
       0
       1
       0
       0 ];

we can extract its dimensions using:

size(A);

Apparently, we can achieve the same things in Julia using:

 size(A)

Just that in Matlab we are able to extract the dimensions in a vector, by using:

[n, m] = size(A);

irrespective to the fact whether A is one or two-dimensional, while in Julia A, size (A) will return only one dimension if A has only one dimension.

How can I do the same thing as in Matlab in Julia, namely, extracting the dimension of A, if A is a vector, in a vector [n m]. Please, take into account that the dimensions of A might vary, i.e. it could have sometimes 1 and sometimes 2 dimensions.

2
What would you expect m to be after the assignment, when the size is to small? - ivarne
I am not sure I understood your question. The size of A will vary with respect to both n and m. I am interested to take m and n to pass them of a function. m would take values between 1 and 3. It's a small model we are talking about. - user3510226
I am completely new to Julia. I may throw a different look at this issue: can I force a vector of dimensions (n,1) to be a matrix of dimensions (n,1)? - user3510226

2 Answers

8
votes
A = zeros(3,5)
sz = size(A)

returns a tuple (3,5). You can refer to specific elements like sz[1]. Alternatively,

m,n = size(A,1), size(A,2)

This works even if A is a column vector (i.e., one-dimensional), returning a value of 1 for n.

6
votes

This will achieve what you're expecting:

n, m = size(A); #or 
(n, m) = size(A);

If size(A) is a one dimensional Tuple, m will not be assigned, while n will receive length(A). Just be sure to catch that error, otherwise your code may stop if running from a script.