Prior to r2020b the height (number of rows) and width (number of columns) of an array or table can be determined by the size function,
array = rand(102, 16);
% Method 1 [dimensions] = size(array); h = dimensions(1); w = dimensions(2);
% Method 2 [h, w] = size(array); %#ok<*ASGLU> % or [h, ~] = size(array); [~, w] = size(array);
% Method 3 h = size(array,1); w = size(array,2);
In r2013b, the height(T) and width(T) functions were introduced to return the size of single dimensions for tables and timetables.
Starting in r2020b, height() and width() can be applied to arrays as an alternative to the size() function.
Continuing from the section above,
h = height(array) % h = 102
w = width(array) % w = 16
height() and width() can also be applied to multidimensional arrays including cell and structure arrays
mdarray = rand(4,3,20); h = height(mdarray) % h = 4
w = width(mdarray) % w = 3
The expanded support of the height() and width() functions means,
- when reading code, you can no longer assume the variable T in height(T) or width(T) refers to a table or timetable
- greater flexibility in expressions such as the these, below
% C is a 1x4 cell array containing 4 matrices with different dimensions rng('default') C = {rand(5,2), rand(2,3), rand(3,4), rand(1,1)}; celldisp(C)
% C{1} = % 0.81472 0.09754 % 0.90579 0.2785 % 0.12699 0.54688 % 0.91338 0.95751 % 0.63236 0.96489 % C{2} = % 0.15761 0.95717 0.80028 % 0.97059 0.48538 0.14189 % C{3} = % 0.42176 0.95949 0.84913 0.75774 % 0.91574 0.65574 0.93399 0.74313 % 0.79221 0.035712 0.67874 0.39223 % C{4} = % 0.65548
What's the max number of rows in C?
maxRows1 = max(cellfun(@height,C)) % using height() % maxRows1 = 5;
maxRows2 = max(cellfun(@(x)size(x,1),C)) % using size() % maxRows2 = 5;
What's the total number of columns in C?
totCols1 = sum(cellfun(@width,C)) % using width() %totCols1 = 10
totCols2 = sum(cellfun(@(x)size(x,2),C)) % using size(x,2) % totCols2 = 10
Attached is a live script containing the content of this post.
A faster version of:
totCols2 = sum(cellfun(@(x)size(x,2),C))
is
totCols2 = sum(cellfun('size', C, 2))
Although 'size' appears in the section "Backward Compatibility" in the documentation, this direct commands are much faster than calling function handles: 'isempty', 'islogical', 'isreal', 'length', 'ndims', 'prodofsize', 'size', 'isclass'
Thanks Jan. Since the string syntax fails with some classes, I avoid using it in demos where users could try to use the same syntax with a class that causes problems. But the benefits in speed of the string syntax is definitely worth it when you know you're dealing with a class that is supported by it.
Rik posted a comment recently that summarizes this well.
For example,
>> C = {rand(2,2), string([])} C = 1×2 cell array {2×2 double} {0×0 string}
>> totCols2 = sum(cellfun('size', C, 2)) totCols2 = 3 % incorrect
>> totCols2 = sum(cellfun(@(x)size(x,2),C)) totCols2 = 2 % correct
Is any equivalent for more than 2D matrices?
Just the good old size(array, n) where n specifies the dimension.
Yes, I prefer this method
Maybe people will finally stop teaching length. Previously my lecturing included only numel and size as alternative, but now I have two functions to provide as readable alternatives.
"Maybe people will finally stop teaching length."
That will be a day to celebrate!
I am yet to find a use-case for length, and much prefer numel and size for their predictability and clarity.
What's the issue with length?
Asking for a friend
I started programming in MATLAB for some analyses in the clinical motion analyses. The trajectories of the measurements have been stored a [nVariables x nTimePoints] matrices. In a subfunction I've determined the number of time points dynamically using LENGTH(), which failed after some years, when I've introduced more variables: Then the number of variables exceeded the width of the array and took some time to find the problem. An equivalent problem occurred at computing the standard deviation between persons: std([nPersons x nVariables x nTimePoints]). A specific group of patients contained one person only and STD() was applied to the 1st non-singelton dimension, which was the 2nd one. The save approach is to specify the dimension to operate on in every case: std(X, 1, 1).
Matlab's "smart" way to guess, which dimension is meant, is a source of bugs. This concerns LENGTH as well as preferring the 1st non-singelton dimension. In short hacks, this saves some seconds for typing, but in productive code for scientific applications it is a good programming practice to specify the dimensions in every case.
I wholeheartedly support your last paragraph. It is similar to my advice for GUI-design: always use specific handles for graphics calls, so the user can click on another figure while your GUI is running, without any major issues.
The issue with length is the unpredictable outcome. You can tell if it will use the height, width, or later dimension. The most common use case will be in a loop:
for n=1:length(A) SomeFunction(A(n)) end
In almost every case you want to loop over the rows or the columns or all elements of a matrix. In most cases you know in advance which you need. If you're lucky length will be equivalent to size(A,1) or size(A,2) or numel(A), but if A is not the exact shape you expected, the result will be different.
If you don't tell Matlab what you mean, it will do what ask.
Good question, Mario.
length() returns the largest array dimension and does not indicate which dimension that is. For vectors, numel() and length() will return the same value but a responsible programmer would have to include additional validation to confirm the input is a vector when using length(). Instead, you can just use size(), numel(), width(), or height() to control which dimension to reference.
For example, if you're setting up a loop to perform an operation on each element of an array and use length() to define the number of iterations, the loop will not be complete if the array has more than 1 dimension.
>> x = rand(2,5); >> length(x) ans = 5 >> numel(x) ans = 10
Another example is if you need to know the number of columns of a matrix that's defined by x=rand(2,20), length(x) may seem like a viable choice because it returns 20 but if the variable is transposed, x=x', length(x) would still return 20 even though the number of columns is now 2.
I haven't found an example where length() is the appropriate choice unless you want to return the max dimension size.
I think it is quite common to know that a variable is a vector, but not be sure whether it is n by 1 or 1 by n The good thing about length() when applying it to a vector, x is that length(x) = length(x') so it doesn't matter if the vector is n by 1 or 1 by n. I suppose that numel() is also safe in this case. In mathematics it is common to refer to the "length" of a vector. This makes code with length() a little more readable as it maps one to one with the common mathematical usage.
So I would say it is good to use either length or numel to determine the number of elements in a vector, but I definitely would not use the new height or width on a vector as it could easily by oriented the wrong way.
Thanks, Jon. But I think the risks outweigh the benefits with length(). In cases where the user wants max(size(x)), they should use that instead of length(x) since the prior is readable and the latter is highly misleading. In cases where people want the length of a vector, numel() has exactly the same output but is more versatile when the variable is unexpectedly multidimensional.
True, "length" is more readable to an inexperienced user than "numel" (number of elements) but an inexperienced user is also more likely to apply length() to a variable that may not always be a vector. Perhaps a concession would be to force the variable to be a vector using length(x(:)) but still, the only benefit to length() over numel() is that it may sound better.
Agreed. I guess I'm just trying to rationalize why I'm still sometimes using length in my code instead of numel. Old habits die hard. I guess I should start consistently using numel. Thanks for all of the interesting (I guess I'm way to far into MATLAB if this is interesting) discussion. I've learned a lot from everyone's posts in MATLAB answers.
Great point, Rik. I meant to include that but forgot. Now you saved me from having to edit it.
Posts by this author
-
Discussion
-
Discussion
-
Poll
-
Discussion
-
Discussion
웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
아시아 태평양
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)