how can i make a function that accepted only squared and positive matrices?
显示 更早的评论
if the user entered a squared matrix it returns 1 and if not it returns 0
3 个评论
KL
2018-4-20
Looks like a homework, did you try anything already? It's easier to answer if you have specific questions.
Hoda Hossam
2018-4-20
Stephen23
2018-4-20
@Hoda Hossam: what have you tried so far?
回答(2 个)
Walter Roberson
2018-4-20
0 个投票
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 个评论
Hoda Hossam
2018-4-20
Walter Roberson
2018-4-20
Why? The homework is only about testing to see if the array is square, not about changing it to become square.
Hoda Hossam
2018-4-20
Walter Roberson
2018-4-20
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
2018-4-20
Walter Roberson
2018-4-20
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 个评论
Walter Roberson
2018-4-20
I'm pretty sure the user is working on homework.
类别
在 帮助中心 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!