Wednesday, 15 July 2015

matrix - matlab: how to compare two matrices to get the indeces of the elements that differs from one to another -



matrix - matlab: how to compare two matrices to get the indeces of the elements that differs from one to another -

i'm using matlab big multidimensional similar matrices , i'd find differences of between them.

the 2 matrices have same size.

here example:

a(:,:,1) = 1 1 1 1 1 1 1 1 1 a(:,:,2) = 1 1 1 1 1 1 1 1 1 a(:,:,3) = 1 1 1 1 1 1 1 1 1 b(:,:,1) = 1 1 99 1 1 99 1 1 1 b(:,:,2) = 1 1 1 1 1 1 1 1 1 b(:,:,3) = 1 1 99 1 1 1 1 1 1

i need function give me indeces of values differs, in illustration :

output = 1 3 1 1 3 3 2 3 1

i know can utilize functions find(b~=a) or find(~ismember(b, a)) don't know how alter output indeces want.

thank all!

you have correct! remember find finds column major indices of in matrix (or vector) boolean status want check beingness satisfied. if want actual row/column/slice locations, need utilize ind2sub. phone call way:

%// reproduce problem = ones(3,3,3); b = ones(3,3,3); b(7:8) = 99; b(25) = 99; %// phone call [row,col,dim] = ind2sub(size(a), find(a ~= b));

the first parameter ind2sub matrix size of you're searching. since dimensions of a equal b, can take either a or b first input, , utilize size help determine size of matrix. sec input column major indices want access matrix. these result of find.

row, col, , dim give rows, columns , slices of elements in 3d matrix not equal. note these column vectors, output of find produce column vector of column-major indices. such, can concatenate each of column vectors single matrix , display information. therefore:

locations = [row col dim]; disp(locations); 1 3 1 2 3 1 1 3 3

as such, first column of matrix tells row locations of matrix values unequal, sec column of matrix tells column locations of matrix values unequal, , 3rd column tells slices of matrix values unequal. therefore, have 3 points in matrix unequal, located @ (1,3,1), (2,3,1) , (1,3,3) respectively. note unsorted due nature of find searches amongst columns of matrix first. if want have sorted have in illustration output, utilize sortrows. if this, get:

sortrows(locations) ans = 1 3 1 1 3 3 2 3 1

matlab matrix find comparison difference

1 comment:

  1. Matrix and Paging digital PA systems work at their best for large public areas (such as Sydney’s Darling Harbour precinct), transport hubs (train stations, airports, bus terminals etc), casinos and other large public venues. These systems also work for multi-site applications across any IT network near or far.
    http://www.tm-systems.com.au/digitalpa/matrix-and-paging/

    ReplyDelete