It represents data of the form [tag number of rat, x coord, y coord] (as im studying a rat population). I would like to calculate the displacement of x & y with respect to the first x & y entries for each of the same value of the tag, i.e. I want an nx3 matrix (where n is size of my data) which gives me [tag number again,(0,x2-x1,x3-x1,x4-x1 etc),(0,y2-y1,y3-y1,y4-y1 etc)] but I am totally inept at programming and am flummoxed right now.

2 Answers
2

I think the easiest approach is to first split your data into a cell array of datasets that contain a single tag only. Then it's just a matter of subtracting the first row from each dataset.

You can use the diff function to find the indices where your first column changes. That would give you all the points at which to split your data. Something like this:

splits = find([1; diff(M(:,1)) ~= 0; 1])

Breaking this down, we say that the first row is always a point of interest. Then any row where the index changes is a point of interest. Finally, the row after the last row is also a point of interest.

Hi, thanks for the help, I think youre right, using cell arrays is a much nicer & easier way. I've had a go at writing the command which subtracts each row of each cell from the first row of that cell, and I think im getting the correct answers, however i'm using a for loop and only the very last calculation is being saved, the rest are just written over during the loop. As a newbie, this is the sort of thing that takes ages to figure out & find a solution to, so would really appreciate more help. Heres the code that im using after I've put my matrix of many tags into a cell array:
–
user2019727Jan 31 '13 at 14:27

here it is: for i=1:size(cellH,1) for k=1:size(cellH{i},1) cellHdisp=cellfun(@minus,{cellH{i,1}(k,2:3)},{cellH{i,1}(1,2:3)},'uniformoutput'‌​, false) end end thanks!
–
user2019727Jan 31 '13 at 14:38

Sorry I had some trouble at formatting my code nicely, I hope you can make sense of it, if not, could you teach me how to format code on SO as well!! Thanks!!
–
user2019727Jan 31 '13 at 15:17

Don't worry. You can't format code in comments =) Don't loop over the matrix in each cell. You can use repmat to repeat the first row N times. Then you can simply subtract that matrix from your original. If you want to preserve the first row and first column, you can operate on a smaller range. Something like A{i}(2:end,2:3) = A{i}(2:end,2:3) - repmat(A{i}(1,2:3), [size(A{i},1)-1, 1]).
–
paddyJan 31 '13 at 18:56