How to rotate without using built-in functions?

Hi, I'm trying to write a function that will take an input matrix and rotate it 180 degrees to provide and output matrix. Here is what I have so far:
function [out]=matrix180(in)
[row,col]=size(in);
for m=1:length(row)
for n=1:length(col)
out(n,m)=in(m,n);
end
end
The problem I keep having is that the matrix returned does not have the same dimensions as the input, i.e. if the input is 465x500, the output is always only 1x1. Additionally, because of this issue, I don't know if the output will actually be rotated by 180 degrees or not. How can I tell how much I am rotating my matrix by looking at my code?

4 件のコメント

Luna
Luna 2019 年 3 月 26 日
You don't need to use length because row and col already 1x1 double which gives you the row and column number. So when you say length(row) -> this gives 1.
Here:
function [out]=matrix180(in)
[row,col]=size(in);
for m=1:row
for n=1:col
out(n,m)=in(m,n);
end
end
Martine Delgado
Martine Delgado 2019 年 3 月 26 日
Ah I see.. thank you! That addressed my size of having a 1x1 matrix. Now how can I rotate it so that I have an input of mxn and an output of nxm...
Walter Roberson
Walter Roberson 2019 年 3 月 26 日
Are you rotating by 90 degrees, or by 180 degrees?
Walter Roberson
Walter Roberson 2019 年 3 月 26 日
Consider
a b c
d e f
g h i
j k l
your code would create
a d g j
b e h k
c f i l
whereas a 90 degree rotation counter-clockwise would produce
c f i l
b e h k
a d g j
and a 180 degree rotation would produce
l k j
i h g
f e d
c b a

サインインしてコメントする。

回答 (1 件)

Walter Roberson
Walter Roberson 2019 年 3 月 26 日

1 投票

Do not loop to length(row) and length(col), loop to row and col . row and col are both scalars, so length() of them is just 1.
Note: your code transposes, not rotates by 90 degrees and certainly not 180 degrees.

質問済み:

2019 年 3 月 26 日

コメント済み:

2019 年 3 月 26 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by