Using find for table values

I have a table that is 1764x1, lets call it T. Each value is either 100, 90, 80 or 73. I want to find the indicies of these values in my table. I have tried using find(T==100) to find these values but then i get the error:
"Operator '==' is not supported for operands of type 'table'."
So I try to convert it using table2array(T) but then T turns into a categorical type and when i try to use find with the convertet T i get the error saying "Invalid types for comparison."
How can I find the indicies of specific values in my table?

1 Comment

I suspect ismember will help. Can you give a small example of your data?
Note that a table contains variables, not values. That means you have to select the correct variable first.

Sign in to comment.

 Accepted Answer

Fredrik Rapphed
Fredrik Rapphed on 8 Dec 2021
Edited: Fredrik Rapphed on 8 Dec 2021
I used made the table into a categorical and then converted it to double then i could use the find function as I showed above.
doubleVals = str2double(cellstr(CategoricalVals));
id=find(doubleVals == 100)

5 Comments

You are shadowing double by using it as a variable name, and you are using find to index an array. You should do neither of those two things.
If you reply to either me or dpb, we can probably help you write more rubust and effective code. You can use a violin to put a nail in a wall, but you should listen to people who are trying to give you a hammer.
Why should i not use find to index an array? Isn't that what it is designed for?
Not really. It is intended to find the indices. If you then want to use those indices to index the array, you could skip a step by using logical indexing:
%why do this
array(find(a==b))
%if you can do this
array(a==b)
Ah right, thank you!
find isn't needed just for indexing and adds the overhead of converting the logical array of the expression in the argument to actual indices along with the overhead to deallocate the logical array and reallocate the double array of indices. It works, yes, but just adds a layer of additional operations that don't add anything for the purpose of only addressing and returning the locations in the subject array that are True in the logical indexing array.
There are instances where one may actually need the indices themselves; it is in those cases for which find should be reserved.
See the "Tips" section in the documenation that expressly mentions the specific use -- I think these should be in the Description section at the top and bolded so folks will actually have to read them to get past the beginning.

Sign in to comment.

More Answers (1)

Rik's second comment is the pertinent one here -- you're appying the function to the entire table, not a variable within the table.
ix=find(T.Var1==100);
will address the variable named 'Var1' -- use the correct name for the variable of interest from your own table, of course, in place.
NB: the other comment as well plus another of my own -- it is often not needed to actually use find and convert the logical expression into actual row numbers --
T.Var1(T.Var1==100)
returns the same elements directly from the logical addressing vector.

Categories

Products

Release

R2020a

Community Treasure Hunt

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

Start Hunting!