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

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.
>> 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개)

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

질문:

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