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 Comments

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
@Hoda Hossam: what have you tried so far?

Sign in to comment.

Answers (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 Comments

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?

Sign in to comment.

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

Tags

Asked:

on 20 Apr 2018

Commented:

on 20 Apr 2018

Community Treasure Hunt

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

Start Hunting!