Matlab storing data and checking conditions

Click For Summary
SUMMARY

This discussion focuses on a MATLAB implementation for storing variable sets that meet specific conditions. The user aims to automate the process of finding and storing combinations of variables [a, b, c, d, e] that satisfy a modular condition, specifically using a loop structure to continue until 21 valid answers are found. The provided code snippets illustrate the use of nested loops and flags to manage the search process. The goal is to enhance efficiency by avoiding manual input and ensuring all valid combinations are recorded in the 'allanswers' array.

PREREQUISITES
  • Understanding of MATLAB programming syntax and structures
  • Familiarity with nested loops and conditional statements in MATLAB
  • Knowledge of modular arithmetic and its application in programming
  • Experience with array manipulation and storage in MATLAB
NEXT STEPS
  • Explore MATLAB array functions for efficient data storage and retrieval
  • Learn about MATLAB's 'isequal' function for comparing arrays
  • Investigate optimization techniques for nested loops in MATLAB
  • Study MATLAB's 'meshgrid' function for generating coordinate matrices
USEFUL FOR

Students, MATLAB programmers, and anyone involved in algorithm development who seeks to automate data collection and condition checking in MATLAB.

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 ·
Replies
8
Views
2K
Replies
3
Views
2K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 14 ·
Replies
14
Views
4K
  • · Replies 1 ·
Replies
1
Views
11K