Home > Enterprise >  Is there a way to parse each row of a matrix in Octave?
Is there a way to parse each row of a matrix in Octave?

Time:11-06

I am new to Octave and I wanted to know if there is a way to parse each row of a matrix and use it individually. Ultimately I want to use the rows to check if they are all vertical to each other (the dot product have to be equal to 0 for two vectors to be vertical to each other) so if you have some ideas about that I would love to hear them. Also I wanted to know if there is a function to determine the length (or the amplitude) of a vector.

Thank you in advance.

CodePudding user response:

If by "parse each row" you mean a loop that takes each row one by one, you only need a for loop over the transposed matrix. This works because the for loop takes successive columns of its argument.

Example:

A = [10 20; 30 40; 50 60];
for row = A.'; % loop over columns of transposed matrix 
   row = row.'; % transpose back to obtain rows of the original matrix
   disp(row); % do whatever you need with each row
end

However, loops can often be avoided in Matlab/Octave, in favour of vectorized code. For the specific case you mention, computing the dot product between each pair of rows of A is the same as computing the matrix product of A times itself transposed:

A*A.'

However, for the general case of a complex matrix, the dot product is defined with a complex conjugate, so you should use the complex-conjugate transpose:

P = A*A';

Now P(m,n) contains the dot product of the n-th and m-th rows of A. The condition you want to test is equivalent to P being a diagonal matrix:

result = isdiag(P); % gives true of false
  • Related