Matlab storing data and checking conditions

AI Thread Summary
The discussion focuses on a MATLAB coding problem where the goal is to store sets of variables [a,b,c,d,e] that meet specific conditions into an array called allanswers. The user seeks a method to automate the process of finding and storing at least 21 valid combinations without manually iterating through each possibility. The current code structure involves nested loops and flags to control the flow, but it lacks clarity on how to efficiently continue searching after finding valid answers. Suggestions for improvement include refining the loop logic and ensuring that the program can effectively check for uniqueness in the stored answers. The overarching aim is to optimize the code to achieve the desired outcome seamlessly.
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
ok, you need to clearly explain what the GOAL of this function is. after that we can help you with a solution.
 
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.
 

Similar threads

Replies
8
Views
2K
Replies
10
Views
2K
Replies
1
Views
3K
Replies
7
Views
3K
Replies
2
Views
2K
Replies
14
Views
4K
Back
Top