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 个评论

Looks like a homework, did you try anything already? It's easier to answer if you have specific questions.
I tried a lot but i couldn't reach a right solution

请先登录,再进行评论。

回答(2 个)

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 个评论

I want to use for loop or while to make number of rows equal to number of columns in the matrix
Why? The homework is only about testing to see if the array is square, not about changing it to become square.
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?
No.. I want the function to accept only equaled rows and columns Such as 3 rows and 3 columns
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

类别

在 帮助中心 和 File Exchange 中查找有关 Matrix Indexing 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by