Matlab storing data and checking conditions

Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
2 replies · 1K views
thestrong
Messages
9
Reaction score
0

Homework Statement


clear all;
close all;
totalanswers=zeros(100,4);
k= ;
flag =0;
for i =0:25
a=i;
if flag ==1
break;
end

and so forth for a,b,c,d, etc for a finite set of values all have the break values too.

x=[ a,b,c,d];

does some math calculationhere is a true statement to see if conditions are met

if (true == 1)
answers=[a,b,c,d]
if (answers ~=[previous ones])
flag = 1;
break;

The Attempt at a Solution



I need a way to store all the set of variables [a,b,c,d] that satisfy the conditions in totalanswers and to continue to run after one has been found.
 
Physics news on Phys.org
clear all;
close all;
k=21;
flag =0;
allanswers=zeros(100,5);

while(allanswers(21,:) ~= [0,0,0,0,0])

%need to add a for loop and flags for each variabled added
for i =0:20
a=i;
if flag ==1
break;
end
for j=0:20
b=j;
if flag == 1
break;
end
for h =0:20
c= h;
if flag == 1
break;
end
for l=0:20
d=l;
if flag ==1
break;
end
for p=0:20
e=p;
%need to add variables when changing k
x=[a,b,c,d,e];

[Xcols, Xrows] = meshgrid(x, x);
G = Xcols-Xrows;
G = G.';
G(logical(eye(size(G)))) = [];
G=mod(G,k);
S=sort(G);

AllElements = zeros(1,k-1);
count = 1;
for i=1:k-1
AllElements(i) = count;
count = count+1;
end

true=isequal(S,AllElements);
if (true == 1)
%need to add variables when changing k
s=[a,b,c,d,e];
s=sort(s);
for i = 1:k
B=allanswers(i,:);
notlisted=isequal(s,B);
if (notlisted == 1)
allanswers(i,:)=s;
break;
end;
end;
flag = 1;
break;
end
end
end
end
end
end
endBasically I want the code to store all of the values into all answers and run until at least 21 "answers" are found instead of doing it manually. I am not sure what would be an effective method to run until 21 [a,b,c,d,e] answers that satisfy the module conditions I set.