How to create comparison matrices "comparing" arrays from an struct array with 4 columns and N rows?
1 回表示 (過去 30 日間)
古いコメントを表示
German Preciat Gonzalez
2016 年 7 月 26 日
コメント済み: Andrei Bobrov
2016 年 7 月 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!