How to create comparison matrices "comparing" arrays from an struct array with 4 columns and N rows?
2 次查看(过去 30 天)
显示 更早的评论
I have data from 4 different sources in a form of arrays inside a struct array. I want to compare which arrays are the same and if they are not equal count how many times they disagree.
So if the first row of the struct array is:
sArray(1).array1={'1' '1' '1'}
sArray(1).array2={'1' '1' '3'}
sArray(1).array3={'1' '1' '1'}
sArray(1).array4={'1' '1' '1'}
arrays 1,3 and 4 are the same, and array 2 is different to all, so I will like to receive a matrix where similar arrays have the same number, and one matrix that counts the number of differences when compared with the first array (the first column should be always zero), like this for the example above:
c(1,:)=[1 2 1 1]
d(1,:)=[0 1 0 0] % normaly I use strcmp
if all the arrays were different I will like to receive something like this:
c(n,:)=[1 2 3 4]
d(n,:)=[0 any# any# any#]
or all the same
c(n,:)=[1 1 1 1]
d(n,:)=[0 0 0 0]
if an array is missing get an NaN
c(n,:)=[1 NaN 1 1]
d(n,:)=[0 NaN 0 0]
so for this struct array:
sArray(1).array1={'1' '1' '1'}
sArray(1).array2={'1' '1' '3'}
sArray(1).array3={'1' '1' '1'}
sArray(1).array4={'1' '1' '1'}
sArray(2).array1={'1' '1' '1'}
sArray(2).array2={''}
sArray(2).array3={'2' '2' '1'}
sArray(2).array4={'2' '2' '1'}
sArray(3).array1={''}
sArray(3).array2={'1' '3' '3'}
sArray(3).array3={'1' '3' '3'}
sArray(3).array4={'2' '2' '1'}
I would like to recieve smoething like:
c = [1 2 1 1;
1 NaN 3 3;
NaN 2 2 4]
Each row represent a comparison of the arrays, columns represent the sources, each of the elements of the matrix can take values form 1-4 or NaN.
Column 1 have values of 1 or NaN
Column 2 have values of 1,2 or NaN
Column 3 have values of 1,2,3 or NaN
Column 4 have values of 1,2,3,4 or NaN
And the differences:
d = [0 1 0 0;
0 NaN 2 2;
NaN NaN NaN NaN] % since the first value is missing
I did that using a lot of IF and the script looks so ugly haha, "If" you can recomend me something I would really appreciate it
采纳的回答
Andrei Bobrov
2016-7-26
编辑:Andrei Bobrov
2016-7-26
sArray (1) .array1 = { '1' '1' '1'}
sArray (1) .array2 = { '1' '1' '3'}
sArray (1) .array3 = { '1' '1' '1'}
sArray (1) .array4 = { '1' '1' '1'}
sArray (2) .array1 = { '1' '1' '1'}
sArray (2) .array2 = { ''}
sArray (2) .array3 = { '2' '2' '1'}
sArray (2) .array4 = { '2' '2' '1'}
sArray (3) .array1 = { ''}
sArray (3) .array2 = { '1' '3' '3'}
sArray (3) .array3 = { '1' '3' '3'}
sArray (3) .array4 = { '2' '2' '1'}
z = struct2cell(sArray);
x = cellfun(@(ii)str2double([ii{:}]),squeeze(z),'un',0);
y = cell2mat(x);
[m,n] = size(y);
c = zeros(n,m);
d = zeros(n,m);
for jj = 1:n
[~,b,c0] = unique(y(:,jj),'first');
c(jj,:) = b(c0);
end
c(isnan(y')) = nan;
d = bsxfun(@minus,c,c(:,1));
4 个评论
Andrei Bobrov
2016-7-30
nm = fieldnames(sArray);
sArray.(nm{structfun(@isempty,sArray)}) = {''};
更多回答(1 个)
Guillaume
2016-7-26
array = num2cell(cellfun(@(c) [c{:}], permute(struct2cell(sArray), [3 1 2]), 'UniformOutput', false), 2)
You can then use Stephen's answer to your previous question.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrices and Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!