Attempt to reference field of non-structure array.
Mostrar comentarios más antiguos
So I'm trying to write a code to count the inversions in an array. So in the array [6,4,3,5,1,2] there are 12 inversions. 6 is greater than 5 of the numbers to the right of it, 4 is greater than three of the numbers to the right of it, and so on. However, I need it so that the function doesn't count zero. So if there was a zero in the place of the 3, 6 would only be greater than 4 of the numbers to the right of it. (zero counts as a space, not as a number. I got some help, and this is the code I have to far
while (a <= array.length)
while (b <= array.length)
if array(a)>array(b);
count = count + 1;
else
count = count + 0;
end
b = b + 1;
end
a = a + 1;
end
When I try to run an example array through it and then try to display the count, it gives me the error "Attempt to reference field of non-structure array." Any help with what I'm doing wrong? Or how to add to the function so it doesn't count being greater as an inversion?
UPDATE: fixing the error of using left in place of right
3 comentarios
Andrew Newell
el 23 de Feb. de 2015
Are you sure you want to compare each number with all of the numbers to the right? I would think that the number of inversions would be 3 (6->4, 4->3 and 5->1).
Image Analyst
el 23 de Feb. de 2015
What's your definition of inversion? And 6 doesn't have any numbers to the left of it. 4 has 1 number to the left of it, not 3. And this isn't .Net - there is no .length method to the variables. Use the length() function.
Respuesta aceptada
Más respuestas (1)
Chris McComb
el 23 de Feb. de 2015
I think that your problem is that array is not a structure. In order to get the length of a, use:
length(array)
instead of:
array.length
1 comentario
Cat
el 24 de Feb. de 2015
Categorías
Más información sobre Performance and Memory en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!