what would the code look like?

Write a function that takes an odd square matrix as an input. This function must then sum the four corner elements of the matrix to a value and multiply said value against the middle column vector of the matrix. The first line of your function should look like this:
function Ans = MatrixFun (A)
this is what i have so far..
in my workspace.... A = [1 2 3;4 5 6;7 8 9]
as a function.....
function Ans = MatrixFun(A)
[c d] = size(A)
Ans = (A(1,1)+A(1,c)+A(c,1)+A(c,c))*A(:,2);
end

5 件のコメント

dpb
dpb 2015 年 3 月 19 日
And the specific question is???
BTW, test your function on
MatrixFun(rand(5))
LG
LG 2015 年 3 月 19 日
we upload the .m files and get feedback.. the function runs and the answer is correct BUT i have not satisfied one of the conditions.....
"Code is not detecting incorrect input ie. It is not detecting if a matrix is even or non-square"
LG
LG 2015 年 3 月 19 日
i have no clue what that means
James Tursa
James Tursa 2015 年 3 月 19 日
編集済み: James Tursa 2015 年 3 月 19 日
Insert a check in your function:
function Ans = MatrixFun(A)
[c d] = size(A);
if( A is even or A is non-square )
error('Input matrix is not even or non-square');
end
Ans = (A(1,1)+A(1,c)+A(c,1)+A(c,c))*A(:,2);
end
You just need to put in the actual code for the "A is even or A is non-square" part. E.g., if you have the size of the matrix in c an d, how can you use those to check if a matrix is non-square? How do you check to see if it is even? Write those conditions out and put them inside the if-check.
dpb
dpb 2015 年 3 月 19 日
>> A=rand(5);
>> [c d]=size(A);
>> Sum= (A(1,1)+A(1,c)+A(c,1)+A(c,c))
Sum =
2.5707
>> Ans = (A(1,1)+A(1,c)+A(c,1)+A(c,c))*A(:,2);
>> [A Ans]
ans =
0.7856 0.0309 0.4671 0.8541 0.6628 0.0794
0.5134 0.9391 0.6482 0.3479 0.3308 2.4143
0.1776 0.3013 0.0252 0.4460 0.8985 0.7746
0.3986 0.2955 0.8422 0.0542 0.1182 0.7597
0.1339 0.3329 0.5590 0.1771 0.9884 0.8559
>>
You're going to try to tell me that 2.57*"MiddleColumnOfA" is the answer shown in the last column above????
I don't think so...

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

回答 (0 件)

カテゴリ

質問済み:

LG
2015 年 3 月 19 日

コメント済み:

dpb
2015 年 3 月 19 日

Community Treasure Hunt

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

Start Hunting!

Translated by