Attempt to reference field of non-structure array.

조회 수: 1 (최근 30일)
Cat
Cat 2015년 2월 22일
편집: Cat 2015년 2월 24일
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
Image Analyst
Image Analyst 2015년 2월 23일
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.
Cat
Cat 2015년 2월 24일
편집: Cat 2015년 2월 24일
So sorry, I meant numbers to the right. And yes, it has to be compared to every number to the right. It's part of a larger function. We are trying to see if a matrix is solvable as a square puzzle. http://www.cs.bham.ac.uk/~mdr/teaching/modules04/java2/TilesSolvability.html here is a link with more information on the inversions and the goal of the function if it helps

댓글을 달려면 로그인하십시오.

채택된 답변

Andrew Newell
Andrew Newell 2015년 2월 24일
Suppose we start with the example from your link:
array = [12 1 10 2 7 11 4 14 5 0 9 15 8 13 6 3];
To eliminate the zero entries, just do this:
array = array(array~=0);
A pretty good way of counting the inversions expresses them as a logical relation, e.g.,
array(2:end) > array(1)
ans =
0 0 0 0 0 0 1 0 0 1 0 1 0 0
Then you can just add up the 1's to get the number of inversions. You can do this in a loop:
count = 0;
for ii=1:length(array)-1
count = count + sum(array(ii+1:end)<array(ii));
end
There are more compact ways of doing this, but they're pretty cryptic.

추가 답변 (1개)

Chris McComb
Chris McComb 2015년 2월 23일
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
Cat
Cat 2015년 2월 24일
Do you have any suggestions for how to not include the zero? I should have put this in the original question. http://www.cs.bham.ac.uk/~mdr/teaching/modules04/java2/TilesSolvability.html This link contains a really good example of the inversions and how it is supposed to play into a bigger function

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by