Can I used Unique to find unique x,y,z coordinates in table data?
Show older comments
I have x,y,z positional data collected and arranged into a table, under two different conditions. For example, I have entries like
x
x1,y1,z1, value1, value 2, ...,0
x2,y1,z1, value3, value4, ...,0
...
x1,y1,z1,value9,value10, ...,1
x2,y1,z1,value11,value 12, ...,1
The last variable in the table is the "different' condition under which the same variables were measured at the same locations.
I want to find the unique x,y,z coordinates in the table and use these to create a summary table that shows the values under the two conditions under which the data was collected. i could use "find" and a vector of coordinates to find these on the table, but I think it would be more elegant if I obtained the unique coordinates from the table, and uses these to find the "other" entry in the table at the same x,y,z location that has the same data collected under the different condition. I've attached some of my data.
for example, I'd like to find the two entries with 1775,575,125 coordinates, and create another table that lists the coordinates, the meanZM and meanGAIpred value from the clamped=1 entry, and the meanInvCurrentfrom the clamped=0 entry.
I'll get sometihng working using "find" in the meantime....
Thank you.
Accepted Answer
More Answers (1)
load('matlab.mat')
T = summaryTable1
[g_idx,result] = findgroups(T(:,[1 2 3]));
vars_1 = {'meanZm','meanGAIpred'};
vars_0 = {'meanInvCurrent'};
N = size(result,1);
vars = [vars_1 vars_0];
result(:,vars) = array2table(NaN(N,numel(vars)));
for ii = 1:N
tmp = T(g_idx == ii,:);
idx = tmp{:,'clamped'} == 1;
if any(idx)
result(ii,vars_1) = tmp(idx,vars_1);
end
if any(~idx)
result(ii,vars_0) = tmp(~idx,vars_0);
end
end
disp(result)
10 Comments
Anyway, to answer the question: Yes, you can use unique.
One way to do that is to replace this line in my answer
[g_idx,result] = findgroups(T(:,[1 2 3]));
with
[result,~,g_idx] = unique(T(:,[1 2 3]),'rows','stable');
which produces this result:
load('matlab.mat')
T = summaryTable1;
[result,~,g_idx] = unique(T(:,[1 2 3]),'rows','stable');
vars_1 = {'meanZm','meanGAIpred'};
vars_0 = {'meanInvCurrent'};
N = size(result,1);
vars = [vars_1 vars_0];
result(:,vars) = array2table(NaN(N,numel(vars)));
for ii = 1:N
tmp = T(g_idx == ii,:);
idx = tmp{:,'clamped'} == 1;
if any(idx)
result(ii,vars_1) = tmp(idx,vars_1);
end
if any(~idx)
result(ii,vars_0) = tmp(~idx,vars_0);
end
end
disp(result)
Another way is to use splitapply and a separate function (called fun for lack of a better term):
[result,~,idx] = unique(T(:,[1 2 3]),'rows','stable');
result = [result array2table(splitapply(@fun,T(:,[7 8 4 9]),idx), ...
'VariableNames',T.Properties.VariableNames([7 8 4]))];
disp(result)
which works the same as when using findgroups for determining the groups:
[g_idx,result] = findgroups(T(:,[1 2 3]));
result = [result array2table(splitapply(@fun,T(:,[7 8 4 9]),idx), ...
'VariableNames',T.Properties.VariableNames([7 8 4]))];
disp(result)
function out = fun(mZ,mG,mI,c)
out = NaN(1,3);
idx = c == 1;
if any(idx)
out([1 2]) = [mZ(idx) mG(idx)];
end
if any(~idx)
out(3) = mI(~idx);
end
end
Jorge
on 5 Sep 2024
load matlab summaryTable1
tS=summaryTable1; clear summaryTable1
tS.Properties.VariableNames=strrep(tS.Properties.VariableNames,'-pos','');
head(tS,3)
tM=varfun(@mean,tS,'GroupingVariables',{'X','Y','Z'},'InputVariables',{'meanZm','meanGAIpred'});
head(tM,5)
Ewww...what's up w/ that???
[nnz(isfinite(tS.meanZm)) height(tS)]
[g,ig]=findgroups(tS.X,tS.Y,tS.Z);
tS(g==1,:)
Indeed, for the first group one of the meanZm values is, indeed, inf.
For convenience, since mean() has an 'omitnan' flag builtin, but not one for 'omitinf', let's fixup the data first and try again...this time we use an anonymous function to be able to set the flag in mean()...
tS.meanZm(isinf(tS.meanZm))=nan;
tM=varfun(@(x)mean(x,'omitnan'),tS,'GroupingVariables',{'X','Y','Z'},'InputVariables',{'meanZm','meanGAIpred'});
tM.Properties.VariableNames=strrep(tM.Properties.VariableNames,'Fun_','Mean_');
head(tM,5)
Voss
on 5 Sep 2024
I want to find the unique x,y,z coordinates in the table and use these to create a summary table that shows the values under the two conditions under which the data was collected... [obtain] the unique coordinates from the table, and uses these to find the "other" entry in the table at the same x,y,z location that has the same data collected under the different condition... for example, I'd like to find the two entries with 1775,575,125 coordinates, and create another table that lists the coordinates, the meanZM and meanGAIpred value from the clamped=1 entry, and the meanInvCurrentfrom the clamped=0 entry.'
?
dpb
on 5 Sep 2024
@Voss - probably, but it might not be very pretty. You can write functions that can output a different number of rows/variables but it can get somewhat messy with varfun when start getting complicated. Sometimes you have to introduce a helper variable in the original table to get to something else indirectly.
Like the other, I'll have to try to find another time to try to do something that involved; I'll have to first study the request more thoroughly because I'm not sure I even have the requirements down yet...that's why I stopped on the original with findgroups and just did the more-or-less trivial exercise above...they're easy! :)
Jorge
on 5 Sep 2024
dpb
on 5 Sep 2024
I just love playing with varfun!!! <vbg> Sorry I do have other pressing commitments that I can only just take quick relief breaks at the moment...
No, it can't be done with varfun, but can be with rowfun. I had forgotten that varfun operates by passing each column of the group one at a time; rowfun passes the groups of rows; all requested columns.
You can do it with your function as
tM=rowfun(@fun,tS,'GroupingVariables',{'X','Y','Z'}, ...
'InputVariables',{'meanZm','meanGAIpred','meanInvCurrent','clamped'});
I rewrote the function a little, but it does the same thing...
function out = jorge(mZ,mG,mI,c) % rowfun passes the variables as separate variables
M=[mZ mG mI c]; % combine into an array
isc=(c==1); % logical addressing for the clamped row
out=[M(isc,1:2) M(~isc,3)]; % pick from the array based on column, clamped
try
assert(width(out)==3); % make sure have three elements
catch
out=[out nan]; % fixup if not...
end
end
The above like @Voss's, assumes there are at most two cases, clamped and unclamped for each position; the fixup wouldn't be necessary if that were universally true; the very last case in the sample file is missing the unclamped case; one presumes that's probably an artifact of the file and not a general case, but if it is real, this gets what is there. It wouldn't work as written if it were the clamped record.
Jorge
on 6 Sep 2024
dpb
on 6 Sep 2024
You're welcome, as noted, I think varfun and rowfun and related functionality with splitapply and/or groupsummary is exceedingly powerful and often not or underappreciated as to just what/how much can be done with them.
And, I think teaching/extending users' acquaintance with the features available is a valuable function of the forum, not just answering a given question...
Categories
Find more on Programming in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!