MATLAB Answers

What would be the best way to return a large array of values as vectors? Each row of the array is a smaller representation of a vector.

5 views (last 30 days)
Daniel E. Loza Morales
Daniel E. Loza Morales on 7 May 2016
Commented: Geoff Hayes on 8 May 2016
If you are a returning visitor to my post, please check my update at the bottom! Thank you for stopping by!
Background : I am trying to create a code that runs through the permutations of the 8 Queens problem on a 8X8 chessboard. And then checking if any are solutions (which is 92). Then checking the solutions all together to see how many unique solutions there are (which is 12). And representing those solutions through the use of a GUI. I have attached two documents that show what I have been working. I have created a set of 8 possible columns (diagonal of ones in a matrix of zeros) since no queens can be on the same row or column. And I extracted each of those columns as a variable "xn" where n is the column number. Then I used the perms command to find all permutations of a vector 1:8, since that is essentially what I would have done with the vectors. The perms vector does not work with the variables (x1, x2, etc.) because they exceed the max dimensions. The second document is a function I created to check if a matrix is a solution.
What I would like to do is preferably write a loop that extracts one row at a time from the variable 'a' and converting 1 to x1, 2 to x2, 3 to x3 and so on. Thus, when transformed a matrix is converted, then that matrix would be checked through my chess_sum8.m function to see if it is a solution. If not then it proceeds to the next row, and so on. If a solution is found I would like to have it displayed as some variable, that way it can be stored easily (like ans1) then when the next answer is found store it as ans2, and so on. Is there any way to make the transformation and easily store each solution as a convenient variable name that changes number for each solution? I haven't gotten to the part of the GUI yet, and hope to solve it on my own, but any help is greatly appreciated, even another method to solve this puzzle. I am fairly new to MATLAB, I have been doing it for about 5 months. But I should be able to solve this with what I currently know, but can't seem to get around it. I tried to do backtracking like in C++ but do not know enough yet to do that method. Thank you for your time. - Daniel E. Loza Morales
Update: I have created a function called transform, which takes a 1X8 matrix. The values entered must be 1-8 and no repetitions or exclusion of one. The first pieces of the random.m is not necessary. I have attached the file. Now, to set up each row of the perms(a) from random.m to run through the transform function. Is there any way to easily label each output from the transform function? That way, the whole program can be done without much manual input.

  1 Comment

Geoff Hayes
Geoff Hayes on 8 May 2016
Daniel - rather than creating named variables (which is what I think that you are trying to do), why not use a cell array which can be used to contain data of varying sizes? (Which again is what I think that you are implying with your different variable of different sizes.)

Sign in to comment.

Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by