Script that performs the same as MATLAB function.

I am looking to create scripts for some internal functions: repmat, fliplr, flipud, and rot90. I am familiar with how each works but I am not sure where I should start. I have two of the four below.
%This script will perform the same operation as rot90
x = input('Enter the matrix: ');
[m,n] = size(x);
[i,j] = length(x);
y = input('Which direction to rotate the matrix\nEnter -90 or 90: ');
newvec = 0; %This is the variable for the result
if y == -90
%rotate counterclockwise
elseif y == 90
%rotate clockwise
end
%This script will duplicate the user entered matrix and create a new matrix
%by the number of rows and columns specfied by the user.
mat = input('Enter your matrix: \n');
rows = input('Enter number of rows: \n');
cols = input('Enter number of columns: \n');
newvec = zeros(rows,cols,mat);

8 comentarios

newvec = zeros(rows,cols,mat); is going to be rejected unless mat is entered as a scalar by the user.
It is not clear what you are asking? What assistance are you hoping for from us?
If you are looking for information on the algorithm to implement these functions: figuring that out is most of the work of the homework assignment.
Hint: look at "for" loops.
David Hughes
David Hughes el 4 de Ag. de 2015
Editada: Walter Roberson el 4 de Ag. de 2015
I rewrote the rot90 script using a for loop but I am not getting the rotation, swapping columns for rows and vice versa. I am unsure how to swap the rows for columns in the direction specified by user.
x = input('Enter the matrix: ');
y = input('Which direction to rotate the matrix\nEnter -90 or 90: ');
[m,n] = size(x);
rows = 0;
cols = 0;
for i = 1:m
for j = 1:n
if y == -90
x(i) = x(j) %rotate counterclockwise
elseif y == 90
x(j) = x(i) %rotate clockwise
end
end
end
David Hughes
David Hughes el 4 de Ag. de 2015
Editada: Walter Roberson el 4 de Ag. de 2015
This is what I got for rotating 90 degrees.
%This script will perform the same operation as rot90
clc
clear all
mat = input('Enter the matrix: ');
drctn = input('Which direction to rotate the matrix\nEnter -90 or 90: ');
[m,n] = size(mat);
fprintf('You entered\n')
disp(mat)
for i = 1:m
if drctn == 90 %Rotate clockwise
mat90(:,i) = mat(m-i+1,:);
elseif drctn == -90 %Rotate counter clockwise
mat90(i,:)= mat(:,m-i+1);
end
end
fprintf('The result is\n')
disp(mat90)
Are you getting an error message with that second script?
The second script, to perform like repmat, does give an error.
And the full text of the error message you receive from your repmat-like script is ... ?
[BTW, I'm assuming you're doing this for homework. If not, I recommend you simply use the functions included with MATLAB.]
David Hughes
David Hughes el 4 de Ag. de 2015
Editada: Walter Roberson el 4 de Ag. de 2015
Yes this is an assignment. The error I was getting was from some changes I made. I've gone back to the beginning and made some additions. I realized I needed to allocate the memory for the resulting matrix. Not getting an error but I am stuck. I am getting the correct size of the resulting matrix but not the correct values. I know that adding i + j in the nested for-loop is not the correct syntax for the correct result. I added i + j as a test and this is where I am stuck.
clear all
clc
mat = input('Enter your matrix: ');
rows = input('Enter number of rows: ');
cols = input('Enter number of columns: ');
[m,n] = size(mat);
newvec = zeros(rows*m,cols*n);
for i = 1:size(newvec)
for j = 1:size(newvec)
newvec2(i,j) = i + j;
end
end
disp(newvec2)

Iniciar sesión para comentar.

Respuestas (0)

Categorías

Más información sobre Creating and Concatenating Matrices en Centro de ayuda y File Exchange.

Preguntada:

el 4 de Ag. de 2015

Editada:

el 4 de Ag. de 2015

Community Treasure Hunt

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

Start Hunting!

Translated by