MATLAB: Matrix Manipulation

matrix manipulation

Hi,
From the following matrix:
A = [1; 1; 3; 1; 2; 1; 1; 1; 3; 1; 1; 1; 1; 2; 1; 1; 1; 1; 1; 3; 1; 1; 1; 2; 1; 1; 1; 1; 1; 2; 2; 1; 1; 1; 2; 1; 1; 2; 1; 1; 1; 1; 1; 1; 3; 1; 1; 3; 2; 1; 1; 1; 1; 1; 1; 1; 2; 2; 4; 1; 1; 2; 1; 1; 1; 1; 2; 2; 1; 1; 1; 3]
I want a matrix of (row numbers minus one) where A is greater than 1. Also, if the element in A is 2 for example then B for that element in A will record the (row numbers minus one) once, if it was a 3 then just twice, and so on; therfore B will look like:-
B = [2, 2, 4, 8, 8, 13, 19, 19,….., 71, 71]
Thanks, any help will be great!

Best Answer

A = [1; 1; 3; 1; 2; 1; 1; 1; 3; 1; 1; 1; 1; 2; 1; 1; 1; 1; 1; 3; 1; 1; 1; 2; 1; 1; 1; 1; 1; 2; 2; 1; 1; 1; 2; 1; 1; 2; 1; 1; 1; 1; 1; 1; 3; 1; 1; 3; 2; 1; 1; 1; 1; 1; 1; 1; 2; 2; 4; 1; 1; 2; 1; 1; 1; 1; 2; 2; 1; 1; 1; 3];
a1=find(A==2)-1; %find the index of the 2
a2=find(A==3)-1; %find the index of the 3
a2=repmat(a2,2,1) %duplicate the 3 indexes
B=sort([a1;a2])'; %join and sort the solution, also transpose
%I transposed B because of your B example