title: Rotation of 3D data in XY plane (Matlab)
body: <p>I have a 3D array which contains probability values as a function of spatial position (x and y coordinates). When I plot this array using imagesc or surf, I get an elliptic profile with a maximum. I would like to rotate the data around the point of maximum probability in the XY plane such that the Y axis becomes parallel to the major axis of the ellipse (which I computed using regionprops). I don't want to rotate the image or the surface using imrotate or geometric object rotation, since I need the array containing the rotated data. How could I achieve this if I know the angle of rotation around the Z axis (obtained with regionprops)?</p>
  
  <p><em>Edit</em>: Here is the code I used to rotate my matrix around the centroïd. However, when I plot the result using surf, I get the weirdest result...</p>
  
  <pre><code>% Translation and anti-translation matrix
  translate = [1 0 0 -centroide(1); 0 1 0 -centroide(2); 0 0 1 0; 0 0 0 1];
  anti_translate = [1 0 0 centroide(1); 0 1 0 centroide(2); 0 0 1 0; 0 0 0 1];
  
  % Rotation matrix
  rotate = [cosd(-orientation) -sind(-orientation) 0 0; sind(-orientation)   cosd(-orientation) 0 0; 0 0 1 0; 0 0 0 1];
  
  % Temporary cell array for data transformation
  data_vector = cell(size(RCb,1),size(RCb,2),size(RCb,3));
  % Cell array for transformed data storage
  transformed_data = cell(size(RCb,3),1);
  for i = 1:(size(RCb,3))
      transformed_data{i} = zeros(3, size(RCb,1)*size(RCb,2));
  end
  for i = 1:size(RCb,3) % temporal dimension
      for j = 1:size(RCb,2) % x dimension 
          for k = 1:size(RCb,1) % y dimension
              % [x y z] vector
              data_vector{k,j,i} = [j,k,RCb(k,j,i),1]';
              % translation
              data_vector{k,j,i} = translate*data_vector{k,j,i};
              % rotation
              data_vector{k,j,i} = rotate*data_vector{k,j,i};
              % anti-translation
              data_vector{k,j,i} = anti_translate*data_vector{k,j,i};
              data_vector{k,j,i} = data_vector{k,j,i}(1:3);
              transformed_data{i}(:,((j-1)*size(RCb,1)+k)) = data_vector{k,j,i};
          end
      end
  end
  
  %%%% Test %%%% : image at 100 ms
  figure,
  imagesc(transformed_data{100}(2,:),transformed_data{100}(1,:),transformed_data{100}(3,:));
  </code></pre>
  
  <p>I get this image:<img src="https://i.stack.imgur.com/CyYaQ.png" alt="enter image description here"></p>
  
  <p>What am I doing wrong?</p>