Is there a better way to write this short function, without the 'for' loop?

function Rows = FindMatchingTableRows(Tbl,VarNames,VarVals)
% Find the numbers of the rows in Tbl with the values
% given in VarVals on the variables given in VarNames.
%
% Tbl is a table.
% VarNames is a cell array of strings naming k variables in Tbl.
% VarVals is an array of k numeric values specifying the desired
% values of the VarNames variables.
% Rows is a vector with the numbers of the Tbl rows having the desired
% values on the indicated variables.
k = numel(VarVals);
Tol = .001;
matching = true(height(Tbl),1);
for kidx=1:k
matching = matching & (abs(Tbl.(VarNames{kidx})-VarVals(kidx))<=Tol);
end
Rows = find(matching);
end
Thanks

 Accepted Answer

mtch=ismembertol(Tbl(:,VarNames),VarVals,Tol);
but will be array, not vector and if return only the vector result of find will have lost the location other than by serial order overall. That may be good enough, depends on need...

6 Comments

Thanks, that seems to do the job, with the minor modification of curly braces: Tbl{:,VarNames}
Huh...thought I had, but see not.
Actually, I accepted the answer too quickly--it doesn't work for all cases. ismembertol gives a positive result if there is a match with the variable matches any of the values--not just the corresponding one.
For example, suppose the values of three relevant variables in a table row are 1, 2, 3. Suppose the values I'm looking for are 2, 3, 1. ismembertol accepts the row because each variable's value is somewhere in the value list, but I don't want that row because the values don't match up in order. Maybe that is what you meant by "lost the location", but I didn't get it at first.
Well, the problem isn't ismembertol; as I pointed out it's up to you to decide what it is that you want and have your function use ismembertol properly for the intended result. Your precise requirement wasn't clear.
The returned value mtch above is a logical array the size of returned array from the selection from the table; which column/row of the original table variables is obtainable from that array but it does take a decoding to transform from the selected array space/size to the specific columns in the table; the row indices of course are the same as the table is rectangular.
To make the selection on a match of each element by position, use the 'ByRows' named input option.
I just did a very small double-check that it works as I thought it did here with the patients data set that I used to test the variable access from the table--
>> load patients
>> p=table(Age,Gender,Height,Weight,Smoker,'RowNames',LastName);
>> pp=p(1:4,{'Age','Height','Weight'})
pp =
4×3 table
Age Height Weight
___ ______ ______
Smith 38 71 176
Johnson 43 69 163
Williams 38 64 131
Jones 40 67 133
>> pp=table2array(pp);
>> ismembertol(pp,[38 69 131],2,'datascale',1,'byrows',0)
ans =
4×3 logical array
1 1 0
0 1 0
1 0 1
1 1 1
>> ismembertol(pp,[38 69 131],2,'datascale',1,'byrows',1)
ans =
4×1 logical array
0
0
0
1
>>
NB: the last only returns the one row as matching all three variables on a 1:1 basis; without it compares each element on its own merit.
Of course, the last result is easily-enough obtained from the former without the 'rows' option as
>> all(ismembertol(pp,[38 69 131],2,'datascale',1),2)
ans =
4×1 logical array
0
0
0
1
>>
Moral: Read the documentation! :)
Thanks, I think I've got it now. 'byrows',1 is the key. Sorry I didn't pick that up from the documentation.
Ayup, if the match is for the group as a whole...that wasn't totally clear initially which is why I was pointing out the result was going to be an array and find would turn the array into a vector so that would only have the serial location in the array coming back...

Sign in to comment.

More Answers (0)

Categories

Asked:

on 24 Apr 2018

Commented:

dpb
on 26 Apr 2018

Community Treasure Hunt

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

Start Hunting!