0

I have a couple of line of code which compares some values in two different matrices and even if it is true it doesn't enter the if part.

for i = 1:ux
    for j = 1:SIR
        if ShelfInfo{SIR, 2} == uniquexy(ux, 1) && uniquexy{ux, 2} == ShelfInfo{SIR, 3}
            shelf = ShelfInfo{j,5};
            shelves = [shelves; shelf];
            1
        end
    end
end

This code is work but it doesn't enter the if part. I believe it is because of the braces. When I changed everything with curly braces I am receiving this error Brace indexing is not supported for variables of this type. When I am changing this braces with parentheses I am receiving this error Undefined operator '==' for input arguments of type 'table'.

I can't find what to do can you help me with it?

Utku
  • 1
  • 1
  • Click in the right margin in the MATLAB Editor, on the line you want to learn more about. You should see a red dot appear. Then when you run the code, the debugger will stop at that line, before executing it. You can then examine the contents of variables by hovering over them. This will help you figure out why the `if` doesn't do what you expect. Read the MATLAB docs on the `==` operator too. It is element-wise when comparing arrays, so the `if` might end up doing something different than you expect. – Cris Luengo Apr 19 '19 at 15:20
  • 1
    Possible duplicate of [Difference between accessing cell elements using curly braces and parentheses](https://stackoverflow.com/questions/9055015/difference-between-accessing-cell-elements-using-curly-braces-and-parentheses) – Cris Luengo Apr 19 '19 at 15:21

2 Answers2

0

()-indexing subsets an array by elements, and works on any type of array.

{}-indexing subsets a cell array, and extracts the cells' contained values. Basically, it "reaches into" the cells and pulls out their contents. It only works on cell arrays, or objects that have overloaded subsref() to provide this behavior.

I'm guessing you're accidentally applying {}-indexing to your uniquexy in one of your references there, when both of them should be ()-indexing:

... uniquexy(ux, 1) && uniquexy{ux, 2} ...
Andrew Janke
  • 23,508
  • 5
  • 56
  • 85
  • I have tried but, it doesn't enter the if part. Also, ShelfInfo is a table but uniquexy is a double array. Maybe my problem because of this. To clarify my want. I just want to compare those values and if they are same I want to run if part. – Utku Apr 19 '19 at 08:51
  • You can't use {}-indexing on a double array, so `uniquexy{ux, 2}` is definitely wrong. Fix that, and then work on figuring out why your `if ...` condition isn't producing the answer you expect. It might help to have your code print out all the values for `ShelfInfo{SIR, 2}`, `uniquexy(ux, 1)`, and so on before the `if` statement so you can see what's happening. – Andrew Janke Apr 19 '19 at 09:15
0

Besides the indexing problem (which depends on the data type of your matrices and would be handy to give as a part of a minimal working example), in your if-statement you do not loop over your array elements. I assume you would want to use indices i and j, instead of SIR and ux (they indicate a fixed position in your arrays). So why do you need the if-statement inside two for-loops then?

May be check these links on accessing array elements depending on array types:

Basic array indexing

Cell vs. structure arrays

Tables