Counting 4 Couple Table Arrangements in MATLAB

Click For Summary

Discussion Overview

The discussion revolves around a MATLAB function intended to count the possible seating arrangements of 4 couples at a table, with specific restrictions on seating. The focus is on debugging the code, particularly related to syntax errors and logical structure.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant describes their attempt to write a function for counting arrangements, mentioning specific conditions such as no man sitting next to his wife or another man.
  • The same participant reports a compilation error related to "end" statements in their code.
  • Another participant suggests replacing 'else if' with 'elseif' to resolve the compilation issue, indicating a misunderstanding of MATLAB syntax.
  • A participant clarifies that a character used in the code was incorrectly copied, stating it was meant to be "<" instead of "[".
  • One participant mentions a separate program they wrote for counting the probability of heads in coin tosses, noting its slow performance with a limit of 100k trials.
  • The participant expresses intent to explore Monte Carlo simulations after resolving the current issue.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the correctness of the original code, but there is agreement on the need for syntax corrections. The discussion remains unresolved regarding the overall functionality of the initial function.

Contextual Notes

Limitations include potential misunderstandings of MATLAB syntax and the need for further debugging to ensure the function operates as intended.

bor0000
Messages
49
Reaction score
0
I tried to write a function that would count all possible combinations that 4 couples could sit at a table assuming no man can sit next to his wife or next to another man. and 1wife and 1 husband sit at fixed positions. my problem doesn't compile and it says i either have too many or too few "end" statements. So please find the mistake:
%4wifes problem: man and wife sit at the corners of the table. others may sit so that no man sits next to his wife or another man
%how many possible arrangements?
function c = fourcouples(trials)
pos=[1,0,0,0,0,0,0,0,0,1];
x=[];
c=0;
count=1;
counter=0;
while count[ trials %set matrix x with all possible combinations

for i=2:9
pos(i)=rand;
if pos(i)[ =.25
pos(i)=1;
else if pos(i)[ =.50
pos(i)=2;
else if pos(i)[ =.75
pos(i)=3;
else
pos(i)=4;
end %of if
end %set random variables, end of forloop1

k=0;
for j=2:8
if pos(j)~=pos(j+1)
k=k+1;
end %end of if
end %end for loop2

if k==7
x=[x;pos]; %augment x by a new arrangement
counter=counter+1; %number of rows of rows of x
end %of if
count=count+1;
end %of while loop
i=1;
y=[];
while i[ =counter %get rid of dupblicate arrangements by setting new matrix y
z=y;
y=[y;x(i,:)];
if det(y)==0
y=z;
else
c=c+1;
end
i=i+1;
end
end
return
 
Physics news on Phys.org
up. also i wrote a program for "count the probability that n tosses will give k heads" , based on doing many random trials. that program did compile. but i found it to be rather slow. i.e. the most it would do was 100k trials.

after the current problem compiles, i'll try to do monte carlo simulations.
 
If you're using Matlab, you want to replace 'else if' with 'elseif'. As it is, Matlab wants to put an if inside your else, which would explain the lack of end statements. And what is ' [ ' ?
 
wow thanks! i'll see if it now compiles based on this "elseif"! "[" was really "<", i just copied it to this forum incorrectly.
 

Similar threads

Replies
5
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 8 ·
Replies
8
Views
3K
Replies
1
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 3 ·
Replies
3
Views
5K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 0 ·
Replies
0
Views
3K
  • · Replies 4 ·
Replies
4
Views
10K