Consider the following matrix:
a=[1,2,3]
therefore
size(a)=[1,3]
I want to assign the second dimension 3 to variable n. What is the most efficient way?
Why are the following not working?
[[],n]=size(a)
or
n= num2cell(size(a)){2}
Consider the following matrix:
a=[1,2,3]
therefore
size(a)=[1,3]
I want to assign the second dimension 3 to variable n. What is the most efficient way?
Why are the following not working?
[[],n]=size(a)
or
n= num2cell(size(a)){2}
This is probably the simplest, and works for a
with any number of dimensions:
n = size(a,2);
If a
is guaranteed to have exactly 2 dimensions, you could also use
[ m, n ] = size(a);
and if you don't need the first variable, in recent versions of Matlab you can write
[ ~, n ] = size(a);
As for the things you have tried:
[[],n]=size(a)
does not work because []
is not a variable to which you can assign anything.
n= num2cell(size(a)){2}
does not work because you can't directly index like that in Matlab. You would need a temporary variable: temp = num2cell(size(a)); n=temp{2}
. Or dispose of num2cell
and do: temp = size(a); n=temp(2)
.