Can anyone help me out with this Matlab code?
I need to write a M-file to generate the transpose of a matrix by using "for...end" loops to implement the transpose. Thanks
Matlab M-file to generate the transpose of a matrix by using" for...end" loops to implement the transpose
The easiest (and most inefficient!) way to do this:
function At = transpose(A)
% Returns the transpose of A
% 1) Number of rows and columns
[nr,nc] = size(A)
% 2) Allocate the matrix:
At = zeros(nc,nr) % notice the change row-%26gt;column
% 3) Loop through the numbers
for i = 1:nr
for j = 1:nc
At[j,i] = A[i,j]
end
end
% End of m-file
The transpose of a matrix is nothing but exchanging rows for columns and vice versa.
Of course, Matlab has a command for computing the transpose in an efficient manner; that command is " ' " (the apostrophe), so if you want Bt to be the transpose of B, just type in Matlab:
%26gt;%26gt; Bt = B'
And that's it.
I wonder... why did they ask you to do this in such an inefficient manner?
viruses
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment