How to create comparison matrices "comparing" arrays from an struct array with 4 columns and N rows?
Show older comments
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
1 Comment
Guillaume
on 26 Jul 2016
Accepted Answer
More Answers (1)
Guillaume
on 26 Jul 2016
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.
Categories
Find more on Data Type Identification 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!