how can i make a function that accepted only squared and positive matrices?
Show older comments
if the user entered a squared matrix it returns 1 and if not it returns 0
3 Comments
KL
on 20 Apr 2018
Looks like a homework, did you try anything already? It's easier to answer if you have specific questions.
Hoda Hossam
on 20 Apr 2018
Stephen23
on 20 Apr 2018
@Hoda Hossam: what have you tried so far?
Answers (2)
Walter Roberson
on 20 Apr 2018
0 votes
Assign the output of size() to a variable. Now, for arrays that you want to accept, how many entries does the size have to have? Can you say anything about the numeric relationship between the size entries?
Question: under what conditions is an empty array to be considered as square?
6 Comments
Hoda Hossam
on 20 Apr 2018
Walter Roberson
on 20 Apr 2018
Why? The homework is only about testing to see if the array is square, not about changing it to become square.
Hoda Hossam
on 20 Apr 2018
Walter Roberson
on 20 Apr 2018
For arrays that you want to accept, how many entries does the size have to have?
Can you say anything about the numeric relationship between the size entries? For example is it a square array if the number of columns is number of rows squared, such as 3 rows and 9 columns?
Hoda Hossam
on 20 Apr 2018
Walter Roberson
on 20 Apr 2018
Okay, so what is the arithmetic connection between the number of rows and number of columns in the case of square matrices?
fun returns true IFF the input is a square matrix with positive values:
>> fun = @(M) ismatrix(M) && diff(size(M))==0 && all(M(:)>=0); >> fun([1,2;3,4]) ans = 1 >> fun([1,2]) ans = 0 >> fun([1,2;3,-4]) ans = 0
1 Comment
Walter Roberson
on 20 Apr 2018
I'm pretty sure the user is working on homework.
Categories
Find more on Matrix Indexing 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!