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

2 Ansichten (letzte 30 Tage)
* 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?

Akzeptierte Antwort

Image Analyst
Image Analyst am 23 Nov. 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 Kommentare
Image Analyst
Image Analyst am 24 Nov. 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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

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

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by