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

2 次查看(过去 30 天)
* 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?

采纳的回答

Image Analyst
Image Analyst 2013-11-23
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 个评论
Image Analyst
Image Analyst 2013-11-24
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.

请先登录,再进行评论。

更多回答(1 个)

Walter Roberson
Walter Roberson 2013-11-23
Hint:
for K = 1 : 20
B(K) = A(K) + 5; %for example
end

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by