Matrix length assignment

Matlab defines a matrix function lengthto return

The length of the largest array size

What is an example of using knowledge of the largest dimension? Knowing the number of rows or columns has an obvious use ... but I don't know why anyone would like the largest dimension, whether they are rows or columns.

thank

+4
source share
4 answers

In fact, most of my code wants to do things exactly once for each row, for each column, or for each element.

Therefore, I usually use one of these

size(M,1)
size(M,2)
numel(V)

In particular, they are independent of length to correspond to the number of elements in the vector !

, { matlab} , - , repeat, while. , .

, length:

  • , - .
  • - , @Mike
+3

, , - length ( ). size(M, n), n-th . , , , , , .

, max(size(M)), , .

, , - . . , , .. . n x m, n , 1, . , -, :

%// good case, where num of rows is 2 or greater
size(mean(rand(2, 4), 1)) %// [1, 4]
size(mean(rand(2, 4)))    %// [1, 4]

%// bad case, where num of rows is 1
size(mean(rand(1, 4), 1)) %// [1, 4]
size(mean(rand(1, 4)))    %// [1, 1], returns the average of that row
+2

B, A, , B zeros, A length, .

+1

- , - . length size(vec,1) size(vec,2), , .

@Dennis Jaheruddin, length MATLAB. numel length . .

length numel , , 100k . 100 . numel . ( MATLAB R2014a) :

enter image description here

Here is a lengthlittle slower, but since it is in the microsecond range, I think it will not be a real difference in speed.

+1
source

Source: https://habr.com/ru/post/1569380/


All Articles