Smartly search between tables

조회 수: 1 (최근 30일)
federico nutarelli
federico nutarelli 2021년 7월 2일
편집: Peter Perkins 2021년 7월 29일
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
federico nutarelli
federico nutarelli 2021년 7월 2일
@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
Peter Perkins
Peter Perkins 2021년 7월 29일
dpb, it sounds like the countries are the variables in these tables, so I would think this is a varfun problem, not rowfun.

댓글을 달려면 로그인하십시오.

답변 (1개)

Peter Perkins
Peter Perkins 2021년 7월 29일
편집: Peter Perkins 2021년 7월 29일
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

카테고리

Help CenterFile Exchange에서 Tables에 대해 자세히 알아보기

태그

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by