ios - compare two arrays and find out changes -


i have got array of 3 arrays, matrix 3x3 named _matrix 1 2 3 4 5 6 7 8 9 end have matrix 3x3 named _newmatrix need function compare 2 matrix , give me index of elements changed, or changed. there straight way solve problem?

array comparison fun problem. nested arrays make more interesting.

the first, important question, kind of output looking for. example (and i'm using 2x2 matrices brevity), want third matrix represents differences?

[[2, 3],  [4, 5]] 

diff'd with

[[3, 6],  [5, 2]] 

could be:

[[1, 3],  [2, -3]] 

that seems reasonable idea. let's @ how might implement that.

- (nsarray *)matrixbyfindingdifferencebetweenfirstmatrix:(nsarray *)firstmatrix secondmatrix:(nsarray *)secondmatrix {     nsmutablearray *differencematrix = [[nsmutablearray alloc] init];     for(int = 0; < firstmatrix.count; i++) {         nsarray *row = [firstmatrix objectatindex:i];         nsarray *secondmatrixrow = [secondmatrix objectatindex:i];         nsmutablearray *differencerow = [[nsmutablearray alloc] init];         for(int j = 0; j < row.count; j++) {             nsnumber *currentvalue = [row objectatindex:j];             nsnumber *newvalue = [secondmatrixrow objectatindex:j];             nsnumber *difference = @(newvalue.intvalue - currentvalue.intvalue);             [differencerow addobject:difference];         }         [differencematrix addobject:[nsarray arraywitharray:differencerow];     }     return [nsarray arraywitharray:differencematrix]; } 

that makes sense. don't know if it's want, core of matrix comparison lies in nested iteration (obviously o(n^2), avoid big matrixes)

edit: getting index paths of changed elements.

- (nsarray *)changedindexpathsbetweenfirstmatrix:(nsarray *)firstmatrix secondmatrix:(nsarray *)secondmatrix {     nsmutablearray *changedindexes = [[nsmutablearray alloc] init];     for(int = 0; < firstmatrix.count; i++) {         nsarray *row = [firstmatrix objectatindex:i];         nsarray *secondmatrixrow = [secondmatrix objectatindex:i];         for(int j = 0; j < row.count; j++) {             nsnumber *currentvalue = [row objectatindex:j];             nsnumber *newvalue = [secondmatrixrow objectatindex:j];             if(![currentvalue isequal:newvalue]) {                 nsindexpath *path = [nsindexpath indexpathforrow:j insection:j]; // row technically "column" in sense of matrixes, , section matrix's row.                 [changedindexes addobject:path];             }         }         [differencematrix addobject:[nsarray arraywitharray:differencerow];     }     return [nsarray arraywitharray:differencematrix]; } 

Comments

Popular posts from this blog

cakephp - simple blog with croogo -

How to group boxplot outliers in gnuplot -

bash - Performing variable substitution in a string -