Write a function in Matlab that would take as input a row matrix.

2 visualizaciones (últimos 30 días)
* Use a for loop to compute the square of each element in A and store the square values in matrix B.
function [B] = Square(A)
* Recall the function file that you created from the command window to take an input A=1:10
* Use plot function to plot B versus A
Can anyone help me understand the question so i can try solving it?
will i be using arrays in this problem? and how will i plot a graph?

Respuesta aceptada

Image Analyst
Image Analyst el 23 de Nov. de 2013
You can do this all in one m-file. Have an m-file called test_square.m and in there have both the test harness (calling function) and the Square function. It will look similar to this:
function test_square()
A = 1:10;
B = Square(A);
plot(A, B, 'ro-');
grid on;
function B = Square(A)
for k = 1 : length(A)
B.......
.... and so on.
In the loop you square each element of A because this is exactly what it told you to do. Then just click the green triangle, or type test_square on the command line, to run the program.
  3 comentarios
Image Analyst
Image Analyst el 24 de Nov. de 2013
r means red. o means to put a little circle at the data point. - means to connect the data points with a solid line between them.
Alnuaimi
Alnuaimi el 25 de Nov. de 2013
Thanks a lot for the help!

Iniciar sesión para comentar.

Más respuestas (1)

Walter Roberson
Walter Roberson el 23 de Nov. de 2013
Hint:
for K = 1 : 20
B(K) = A(K) + 5; %for example
end

Categorías

Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by