MATLAB Answers

Smartly search between tables

1 view (last 30 days)
federico nutarelli
federico nutarelli on 2 Jul 2021
Edited: Peter Perkins on 29 Jul 2021
Hi all,
so I have two datasets made of 0 and 1. The two datasets (in the form of matlab tables), call them D1 and D2 are 1229x119. D1 contains true values and D2 predictions. Each column of D1 and D2 has a country name c1,c2,c3,...,c119. What I would like to do is to recover, for each country in a subset, say [c3,c6,c12,c45,c117], the row index for these countries whenever D2 has entry 1 and D1 has entry 0.
The desired output is a cell array indexed by country (say row_index{ck} for k=3,6,12,45,117) whose values are the row indices for which D2=1 and D1=0, s for instance say this happens fr rows 39, 678, 1220 for cuntry c6, I would like too obtain:
row_index{c6}=[39,678,1220]
Could you please help me out on this?
  3 Comments
Peter Perkins
Peter Perkins on 29 Jul 2021
dpb, it sounds like the countries are the variables in these tables, so I would think this is a varfun problem, not rowfun.

Sign in to comment.

Answers (1)

Peter Perkins
Peter Perkins on 29 Jul 2021
Edited: Peter Perkins on 29 Jul 2021
This would be trivial if varfun accepted more than one table input, but it doesn't do that. But you can make one table that looks for your condition, and apply varfun to that:
>> D1 = array2table(randi([0 1],10,3),"VariableNames",["US" "UK" "UAE"]);
>> D2 = array2table(randi([0 1],10,3),"VariableNames",["US" "UK" "UAE"]);
>> D12 = array2table(D1{:,:} & ~D2{:,:})
D12 =
10×3 table
Var1 Var2 Var3
_____ _____ _____
true true false
false false false
true false false
false false false
false false false
false false true
false true true
true true true
true true true
false true false
>> varfun(@(x) find(x), D12, "OutputFormat","cell")
ans =
1×3 cell array
{4×1 double} {5×1 double} {4×1 double}
1229x119 isn't gigantic, so probably no memory issues there. But alternatively,
for i = 1:width(D1)
c{i} = find(D1.(i) & ~D2.(i));
end

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!