Matlab code running loops to meet conditions

Click For Summary

Discussion Overview

The discussion revolves around a MATLAB coding problem where a user is attempting to generate a set of numbers that meet specific conditions related to the Grassmannian equations. The focus is on ensuring that five variables (a, b, c, d, e) yield all numbers from 1 to 20 in modular 21, without any duplicates among the variables.

Discussion Character

  • Technical explanation
  • Homework-related
  • Debate/contested

Main Points Raised

  • The user describes their goal of generating numbers from the set {0, 1, 2, ..., 20} and shares their MATLAB code, which currently does not produce any output.
  • Some participants suggest improvements to the code formatting and indentation for better readability.
  • One participant asks what the expected output of the code is, prompting clarification from the user.
  • The user explains that the code should stop when the condition of generating the complete set G = {1, 2, 3, ..., 20} is met, and they want to retrieve the values of a, b, c, d, e.
  • Another participant points out that the values of a, b, c, d, e are stored in the 'answers' variable and suggests printing it to see the results.
  • The user reports that their workspace is empty when they attempt to run the code, indicating a possible issue with the code execution.
  • One participant advises checking the while loop and suggests that it may not be necessary, warning about the potential for infinite loops.
  • The user mentions removing the while loop but encounters an issue where the sorted output S contains duplicates instead of the expected unique values.
  • The user later reports that they resolved their issue, although the specifics of the resolution are not detailed.

Areas of Agreement / Disagreement

Participants generally agree on the need for code corrections and improvements, but there are unresolved issues regarding the output and the logic of the code. The discussion does not reach a consensus on the best approach to solve the problem.

Contextual Notes

There are limitations in the code related to the handling of variable values and potential infinite loops. The discussion highlights the need for clarity in variable assignments and output expectations.

thestrong
Messages
9
Reaction score
0

Homework Statement


This is not homework but I am trying to solve a problem to see to general a set of numbers for the grassmanian equations to use in something else.[/B]

I have five variables a,b,c,d,e. a-b,a-c,a-d,a-e,b-a,b-c,...etc so that none of them are the same so it generates all numbers from 1,2,3,4,...20 in modular 21. Clearly a,b,c,d,e can not be the same since it will obviously not generate all the numbers. Below is my MATLAB code.

a,b,c,d,e must be in the {0,1,2,3,...20}

The code does not even output anything.

The Attempt at a Solution


Matlab:
clear all;
close all;
k=21;
flag =0;
for i =0:16
a=i;
if flag ==1
  break;
end
  for j=1:17
b=j;
if flag == 1
  break;
end
  for h =2:18
c= h;
if flag == 1
  break;
end
  for l=3:19
d=l;
if flag ==1
  break;
end
  for p=4:20
e=p;

g1=a-b;
g2=a-c;
g3=a-d;
g4=a-e;
g5=b-a;
g6=b-c;
g7=b-d;
g8=b-e;
g9=c-a;
g10=c-b;
g11=c-d;
g12=c-e;
g13=d-a;
g14=d-b;
g15=d-c;
g16=d-e;
g17=e-a;
g18=e-b;
g19=e-c;
g20=e-d;
G=[ mod(g1,k), mod(g2,k), mod(g3,k), mod(g4,k), mod(g5,k), mod(g6,k), mod(g7,k), mod(g8,k), mod(g9,k), mod(g10,k), mod(g11,k), mod(g12,k), mod(g13,k), mod(g14,k), mod(g15,k), mod(g16,k), mod(g17,k), mod(g18,k), mod(g19,k), mod(g20,k)];
S=sort(G);
AllElements=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20];
true=isequal(S,AllElements);
if (true == 1)
  answers=[a,b,c,d,e];
  flag = 1;
  break;
end
  end
  end
  end
  end
end
 
Last edited:
Physics news on Phys.org
I added code tags to your listing for better preaentation.

However it would be better if you can fix the indenting too.
 
what is the code supposed to output?
 
Oh yeah I forgot to code that. It is suppose to just stop when the condition is met that the entire G = {1,2,3,4,5,...20} is generated from the values of a,b,c,d,e and I just want the values of a,b,c,d,e.
 
the values of abcd & e are stored in your answers variable...
just print it...
or add a line below that says
answers
 
I tried that, but when I run the code my workspace is empty.
 
that is true. So there is one way to clear your workstation, whcih is to use the clear all command
you have code after it, there the code must not run.

If I were you I would check my while loop, or remove it all together, infinite loops are no fun
 
I removed the while loop, which I do not think is actually necessary. It does have a workspace, but the code is breaking at an incorrect solution of
S =

1 1 1 1 2 2 2 3 3 4 17 18 18 19 19 19 20 20 20 20. When it should be S = 1 2 3 ...20.
 
I resolved my issue. Thanks!
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 7 ·
Replies
7
Views
2K
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
7K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 15 ·
Replies
15
Views
4K