i created function works on 3*3 matrix how i make it work on 9*9 mat with loop?

1 view (last 30 days)
hey guys,i created function works on 3*3 matrix how i make it work on 9*9 mat with loop?
  6 Comments
ali egbarie
ali egbarie on 14 Mar 2019
the output should be extended to work for larger matrices and still return a true/false

Sign in to comment.

Answers (1)

Rik
Rik on 14 Mar 2019
Edited: Rik on 17 Mar 2019
This should work better:
function tf=checkunit(a,tol)
%Check matrix for unique values
%This function returns true when no duplicates are found (within a default
%tolerance of 10^-6)
if nargin==1,tol=10^-6;end
unique_vals=uniquetol(a,tol, 'DataScale', 1);
%If there are no duplicate values, the number of unique values is equal to
%the number of values in the original matrix.
tf= numel(a)~=numel(unique_vals);
end
One of the problems is that you overwrote n with 1 just before you attempted to use it to determine how many iterations to test.
If you insist on using a for loop, you could try something like this:
function n=checkunit(a)
%the function check if there number showing 2 times in matrix if true n=1,false=0
n=1;
for ii=1:numel(a)
if (find(a==a(ii))~=1)
n=0;
break;
end
end
  1 Comment
Rik
Rik on 20 Mar 2019
Did this suggestion solve your problem? If so, please consider marking it as accepted answer. It will make it easier for other people with the same question to find an answer. If this didn't solve your question, please comment with what problems you are still having.

Sign in to comment.

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!