what would the code look like?
1 view (last 30 days)
Show older comments
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 Comments
James Tursa
on 19 Mar 2015
Edited: James Tursa
on 19 Mar 2015
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
on 19 Mar 2015
>> 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...
Answers (0)
See Also
Categories
Find more on Multidimensional Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!