Smartly search between tables

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

dpb
dpb on 2 Jul 2021
Edited: dpb on 2 Jul 2021
That's a straightforward logical addressing expression, but I'd suggest you may not need to build the explicit index but use rowfun with the Country as grouping variable and the anonymous (or m-file) function operating on the selected subset doing the conditional test on the D variables -- or just build a D1D2 variable that is the logical AND of the two which would be (D1==0).*(D2==1)
@dpb thank you for the reply. Actually I am new to MATLAB. I am using the follwing code:
[row_indices(k),col_indices(k),vals(k)] = find(mcv(:,rich_countries(k))=1 & ttv(:,rich_countries(k))=0);
but it doess not seem to work. Here D1 is mcv and D2 is tttv. rich_countries(k) represents the subset of countries
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

Categories

Products

Release

R2020b

Asked:

on 2 Jul 2021

Edited:

on 29 Jul 2021

Community Treasure Hunt

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

Start Hunting!