%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % handy data manipulation commands %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% a=[1 2 3 4]; % fun with repmat: A1=repmat(a,3,1); A2=repmat(a,1,3); A3=repmat(a,3,2); %fun with reshape B1=reshape(A1,12,1); B2=reshape(A1,6,2); B3=reshape(A1,4,3); %Practice: P1=reshape(A1',12,1); P2=reshape(A1',2,6); P3=repmat(reshape(A1',2,6),3,1); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % indexing %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %Vector example %%%%%%%%%%%%%%%%% a=[11 2 6.7 0] f1=find(a==0); %note the double-equal sign! f2=find(a>2); f3=find(isnan(a)); % find all elements that are "not numbers", %i.e. unidentified, such as might result from a divisin by 0. f4=find(isinf(a)); %find all elements that are "infinity", % such as might result when you take the exponent of an already large number b=a;% just to preserve the original a b(f1)=[]; %delete all f1-indexed elements b=a; b=a(f2); %keep all f2-indexed elements b=a; b(f2)=15; %convert all f2-indexed elements to "15" %direct indexing b=a; b(b==0)=[] b=a(a~=0); b=a(a>2); %Matrix example %%%%%%%%%%%%%%%%%%%%%%%% A=[1 0 3 0; 0 11 0 0; 2 5 11 0]'; f1=find(A==0); [r1,c1]=find(A==0); %returns row and column index for indexed elements [r1 c1]; %makes explicit where exactly indexed elements are located % deleting rows: f=find(A(:,1)==0); A(f,:)=[]; %practice: M=3*[repmat((1:1:3)',2,1) [1 2 3 4 5 6]' ones(6,1)]; f=find(M(:,2)>9); M1=M; M1(f,2)=100; M2=M; M2(f,3)=100; M3=M; M3(f,:)=[]; M4=M; M4(M4==3)=0