Equivalent length() de matlab

Bonjour,

Y a t’il une fonction quivalente length() dans Xojo: en matlab, length(x) renvoie le nombre le plus lev que constitue une Array().

The statement length(X) is equivalent to max(size(X)) for nonempty arrays and 0 for empty arrays.

n = length(X) returns the size of the longest dimension of X. If X is a vector, this is the same as its length

x = Array(1,11,6)
n = length(x)

n = 11

Bonjour,

Non, pas vraiment. Il faudrait utiliser Ubound sur chaque dimension de l’array et retourner le numero le plus haut. Ubound a un deuxieme parameter (optional) ave lequel on peut especifiquer la dimension dont on veut calculer la grandeur.

Since my French is getting too rusty for the forum, I’ll reply in English too. There isn’t a direct equivalent of that function. You wiil need to use Ubound to query the size of each dimension of the array, and then get the maximum.

If you always know the number of dimensions of the array it’s easy, just something like this:

[code]Dim mLength,i as integer

For i= 1 to NumberOfDimensions
mLength=max(Ubound(MyArray,i),mLength)
Next i[/code]

If you don’t know the number of dimensions (is this even possible?), maybe a try-catch block could be the solution (I have never done this, I am not sure it is correct, just an idea):

[code]Try
dim mLength,i as integer

while true
mLength=max(Ubound(MyArray,i),mLength)
wend

catch err as OutOfboundsException
Return mLength

end Try
[/code]

Pour savior combien de dimensions il y a, on emploie UBound avec deuxime paramtre -1. Donc:

dim valeur, k as integer valeur = -1 For k = ubound(tableau,-1) downto 1 valeur = max(valeur,ubound(tableau,k)) Next return valeur+1 //UBound donne l'indice du dernier lment, qui est infrieur la quantit d'lments par 1 puisque les Array de Xojo sont compts partir de 0

Il y-a tout pour en faire une fonction Length() qui retourne le nombre de dimensions et la valeur max.
Merci a tous pour vos interventions.

[quote]

dim tb() as integer = array(1,11,6)

dim mx,i as integer

for each i in tb

if i > mx then

mx = i

end if

next

System.DebugLog "max value = " + mx.ToText[/quote]

Je viens de vrifier la fonction Length qui en fin de compte doit retourner le n de la plus grande valeur auquel est associe la variable tableau, cad entre 1 et N.