Matrix Manipulation Using Matlab Looping Function

Matrix manipulation is the power of Matlab. That’s why Matlab stands for Matrix Laboratory (not Mathematics laboratory). This software easily calculates many Matrix operation that difficult for other languages (c++, java, etc.). In addition, Matlab not only operates through its function but also uses looping tools similar to other language, i.e. ‘for-next’, ‘while-do’, etc. One of my best friends studying in Australia discussed about how to manipulate Matrix efficiently weeks ago. For example we have a matrix:

And we have another matrix, B=[1 2 3 4 5;6 7 8 9 10;11 12 13 14 15];

The question is how to create matrix C which contains column elements in B for every even number of column in A. Of course we can create manually based on the B value, but the problem is when the number of Matrix A and B are thousands, e.g. coordinates. In Matlab we are allowed to use “for-next” instruction to manipulate a matrix.

  • >> C=A;
  • for i=1:10
  • [rows cols]=size(B);
  • if cols>=i
  • C(:,2*i)=B(:,i);
  • end
  • end

The script will give the result:

Use function “size” to know maximum column of A and B. Of course you can try to create the other algorithms for this purpose. Tq