How to create comparison matrices "comparing" arrays from an struct array with 4 columns and N rows?

2 visualizzazioni (ultimi 30 giorni)
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

Risposta accettata

Andrei Bobrov
Andrei Bobrov il 26 Lug 2016
Modificato: Andrei Bobrov il 26 Lug 2016
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 Commenti
German Preciat Gonzalez
German Preciat Gonzalez il 29 Lug 2016
I tried with the two arrays but when I have an empty array as sArray.array3
sArray.array1={'1' '2' '3' '4' '5' '6' '7' '8' '9' '9' '9' '12' '13' '14' '15' '16'...
'17' '17' '2' '3' '16' '1' '4' '5' '6' '7' '8' '9' '9' '9' '12' '13'...
'14' '15'};
sArray.array2={'1' '2' '3' '4' '5' '6' '7' '8' '9' '9' '9' '12' '13' '14' '15' '16'...
'17' '17' '1' '16' '3' '2' '4' '5' '6' '7' '8' '9' '9' '9' '12' '13'...
'14' '15'};
sArray.array3=[];
I receive an error:
Error using cell/unique (line 85)
Input A must be a cell array of strings.

Accedi per commentare.

Più risposte (1)

Guillaume
Guillaume il 26 Lug 2016
To transform your structure to the cell array format you had in your previous question:
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.

Categorie

Scopri di più su Matrices and Arrays in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by