필터 지우기
필터 지우기

if loop dosen't complete?

조회 수: 1 (최근 30일)
daniel slama
daniel slama 2022년 6월 18일
댓글: daniel slama 2022년 6월 18일
hey everyone! so i'm working on a function in mathlab that's supposed to create a matrix with random intergers that are non repeating. also if the user wants to have all intergers as evens he can. if the matrix cannot be created the function will say so.
anyway, in the code below it seems like the function has no issues telling me when the matrix can't be created but when the matrix can be created all i get in return is an empty matrix, i removed the ";" sign to see how matlab is attempting to build this matrix but it seems like it nothing happens. it just returns an empty matrix. what did i do wrong here?
thanks for all the helpers!!
function [A] = ex1(n,m,EVENONLY,p,q)
%creates a nXm matrix of random,non-repeating intergers
%that range from p to q. if EVENONLY is set to true the matrix will only
%have even numbers
A=[];
howmany=m*n;
set=[p:1:q];
if howmany>size(set)
disp('this maatrix is impossible to create 1')
elseif howmany>(0.5*size(set)) & EVENONLY==true
disp('this matrix is impossible to create 2')
elseif howmany<=0.5*size(set) & EVENONLY==true
set=set(mod(set,2)==0)
for index=(1:1:n)
a=set(randperm(q,m))
A=[A;a]
end
elseif howmany<=size(set) & EVENONLY==false
for index=(1:1:n)
a=set(randperm(q,m))
A=[A;a]
end
end
return
end

채택된 답변

DGM
DGM 2022년 6월 18일
size() returns a variable-length vector. Don't compare against the size() of an array if you're trying to compare against a scalar.
if howmany>size(set)
% this will only be entered if howmany is greater than
% the size of all dimensions of the array set
end
What you're trying to check is the number of elements. Use numel() instead of size().
  댓글 수: 1
daniel slama
daniel slama 2022년 6월 18일
lol, i've been trying to figure out what's wrong for so long and it was that simple.
TYSM!!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by