matlab - difference of each two elements of a column in the matrix -


i have matrix this:

fd =   x   y   z 2   5   10 2   6   10 3   5   11 3   9   11 4   3   11 4   9   12 5   4   12 5   7   13 6   1   13 6   5   13 

i have 2 parts of problem:

1) want calculate difference of each 2 elements in column. tried following code:

for i= 1:10  n=10-i;  j=1:n      sdiff1 = diff([fd(i,1); fd(i+j,1)],1,1);     sdiff2 = diff([fd(i,2); fd(i+j,2)],1,1);     sdiff3 = diff([fd(i,3); fd(i+j,3)],1,1);  end  end 

i want differences such as:

  x1-x2, x1-x3, x1-x4....x1-x10   x2-x3,  x2-x4.....x2-x10   .   .   .   .   .   x9-x10 

same y , z value differences

then values should stored in sdiff1, sdiff2 , sdiff3

2) want next same z values, want keep original data points. different z values, want merge points close each other. close mean,

if abs(sdiff3)== 0 keep original data  abs(sdiff3) > 1 if abs(sdiff1) < 2 & abs(sdiff2) < 2 

then need mean x, mean y , mean z of points.

so tried whole programme as:

for i= 1:10      n=10-i;      j=1:n      sdiff1 = diff([fd(i,1); fd(i+j,1)],1,1);     sdiff2 = diff([fd(i,2); fd(i+j,2)],1,1);     sdiff3 = diff([fd(i,3); fd(i+j,3)],1,1);         if (abs(sdiff3(:,1)))> 1                  continue      mask1 = (abs(sdiff1(:,1)) < 2) & (abs(sdiff2(:,1)) < 2) & (abs(sdiff3:,1)) > 1);     subs1 = cumsum(~mask1);      xmean1 = accumarray(subs1,fd(:,1),[],@mean);     ymean1 = accumarray(subs1,fd(:,2),[],@mean);     zmean1 = accumarray(subs1,fd(:,3),[],@mean);     fd = [xmean1(subs1) ymean1(subs1) zmean1(subs1)];          end      end  end 

my final output should be:

2.5   5   10.5 3.5   9   11.5 5     4   12 5     7   13 6     1   13 

where, (1,2,3),(4,6),(5,7,10) points merged mean position (according threshold difference <2) whereas 8 , 9th point has original data.

i stuck in finding differences each 2 elements of column , storing them. code not giving me desired output. can please help? in advance.

this can simplified using vectorised notation. can instance

fd(:,1) - fd(:,2) 

to difference between columns 1 , 2 (or equivalently diff(fd(:,[1 2]), 1, 2)). can make more elegant/harder read , debug pdist if have 3 columns it's more trouble it's worth.

i suspect first problem third argument diff. if use diff(x, 1, 1) first order diff in direction 1, between adjacent rows (downwards). diff(x, 1, 2) between adjacent columns (rightwards), want. matlab uses opposite convention spreadsheets in indexes rows first columns.

once have diffs can test elements:

thesame = find(sdiff3 < 2); % example 

this yield vector of row indices of sdiff3 value less 2. can use

fd(thesame,:) 

to select elements of fd @ indexes. remove matching rows opposite test

notthesame = find(sdiff > 2); 

to find ones keep, extract new array

keepers = fd(notthesame,:); 

these won't give exact solution it'll on right track. syntax of these commands , lots of examples can run e.g. doc diff in command window.


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -