File Exchange

image thumbnail

Rubik's Cube Simulator and Solver

version (2.44 MB) by Joren Heit
This program simulates, manipulates and solves all rubik's cubes. Includes webcam recognition.


Updated 23 Oct 2011

View Version History

View License

Run: >>digrub

This program allows you to generate a randomly scramble cube of arbitrary dimension which can then be manipulated manually or solved by the computer. You can also input your own state using a webcam (3x3x3), or simply enter the colors of each facelet (2,3,4x.x.).

There are several built-in solving mechanisms available:

- God's Algorithm for the 2x2x2: this is the optimal solution for the given state (in half-turn metric).

- Thistlethwaite 45 (T45) for the 3x3x3: this algorithm will always find a solution of 45 moves or less, averaging at 31.

- Layer-by-Layer (Beginners') Solution: this is the method commonly used by beginners to solve the cube. More intuitive than T45, but also more extensive and less effective.

- 423T45 for the 4x4x4 (read 4 to 3, T45): this algorithm brings the cube to a state which can be handled like it was a 3x3x3 cube. When this is achieved, T45 can be applied to solve it (~180 moves on avg).

- Inverse Scramble for all cubes: it is like cheating, but when the scramble is known, each cube can be solved by inversing the sequence.

All of the above methods (with exception of the inverse scramble, which is trivial) are explained extensively in the included PDF. The PDF also contains a vast theoretical description of the cube.

algrot.m - calculates how an algorithm changes under rotation of the cube.
digrub.fig - GUI figure main program
digrub.m - GUIDE-generated m-file to go with digrub.fig.
editstate.fig - GUI figure for manual input program
editstate.m - GUIDE-generated m-file to go with editstate.fig.
findpeeks.m - Peek-finder that is used to find the cube's position in a webcam image.
GetCorners.m - calculates the corner permutation/orientation of a given 3x3x3 cube.
GetCorners2.m - calculates the corner permutation/orientation of a given 2x2x2 cube.
GetEdges.m - calculates the edge permutation/orientation of a given 3x3x3 cube.
GetFacelets.m - converts a state in the orientation/permutation representation to the facelet repr.
Ind2State.m - converts an index to a state
move2rub.m - converts a move of the form 'x11' to Rubik's Code 'B'.
parity.m - calculates the parity of a permutation.
rub2move.m - converts a move in Rubik's code to axile 'x11' form.
rubcheck.m - checks the validity of a 2x2x2 or 3x3x3 state.
rubcross.m - searches for a cross on the cube.
rubgen.m - generates a cube
rubinfo.m - returns information about the cube state
rubopt.m - optimizes a move-sequence
ruborient.m - orients the cube
rubplot.m - plot a 3D image of the cube
rubrot.m - apply a move to the cube
rubrot2.m - rotate the entire cube
rubsolve.m - solve the cube using a layer by layer approach
Solve45.m - solve the cube using T45
Solve222.m - find God's Algorithm to a 2x2x2 cube
Solve444.m - find a solution to any 4x4x4 cube.

Thanks for downloading! All feedback is appreciated!


Cite As

Joren Heit (2021). Rubik's Cube Simulator and Solver (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (66)

Haider Ahmed

Surya Pisini

Tahir Afareen


Herren Thomas D'Souza


Vijay Reddy

how to use this?

Lavanith Togaru

Charles Culberson

Alberto Mora

Very nice and funny function!


Can i use it with windows 10??


Sameel Chaudry


juad ali

Phyu Win

Hello.Why am i getting error in running digrub.m file.plz help me.

mehmet kurga

Ahmed Elkadey

Awesome work

Akshay Kumar

tell me how to control hardware by this code which process can i use


Richard Obler

Maybe add in description: Requires also Image Acquisition Toolbox for the webcam features.

Richard Obler


Waseem AKRAM Channa

Very Nice Work!

Roshni gupta

In Solve444.m I am getting an error Error using Solve444 (line 30)
Not enough input arguments.
Line 30 is x = reshape(R([2 3],[2 3],:),4,6);
What modification I need to do

Giuseppe Lumia

Very nice! I was thinking to realize something like this by myself but then I found your code :)

Alejandro San Blas

Tran Ngoc Nhat

excellent work,,i want contact with you >>>??

Xin Tu

This is so helpful!!!! THANKS

Atul Varhsney

Huy Tran

Thanks very much!!! Excellent work!

kibrom esayas

Anubhab Pal

DangDang Lang

excellent work

shadi halabi

how to i send the string solution to my arduino ?

inzamam ahmed

It is not taking input from WEBcam ?? Please Help me out ....

WAWAN kurniawan

Great mate, its wonderful.


you get an error when opening the "digrub.fig" file. However, if you open "digrub.m" and run it works perfectly. At least it did to me. Great Work @JorenHeit!!!

Tyson Mann

did not work, fix pls.

inzamam ahmed

hello I am getting error while running digrub, error is because of rubgen function. I am using 2013a vesion. Please help me out.


Hello, I am getting errors while running digrub. I am using 2016a version

hola,pero no se como se descarga

ninior ninio

thanks very much.really wonderful

Hammad ur rehman

KEO Sothun

i don't understand !!! :(

Manjunath M C

Shubham Bhatt

thanks for the code.i m looking for this


Nwaobasi Chukwuemeka



Super nice !

Joren Heit

@Kuifeng It's been a long time since I worked on this project, but I can remember trying to add arrows to represent those axes... However, drawing (nice) arrows in Matlab proved not to be as straightforward as you might think, and I gave up (also because of the low priority).


Very nice work, consider adding X-Y-Z arrows in the cube?

Joren Heit

@Krisztian You might wanna give the big blue button that says "Download Submission" a try...

Krisztian Makar

How can i download it?

Abdallah Elsherif

very nice work, thanks for sharing the code


Joren Heit

"Download All" perhaps?

Avinash k

How to download??



congratulations! you are a genius! this is really nice of to share the code...thx!


Mr Smart

Nice, Thanks for share .

Gurinder pal singh

done , its nice


prashant kumar


I was searching for such a program for a long time...I knew how to solve the Cube but for a long wanted to see a program that could implement it....really nice work

vahid veisy

MATLAB Release Compatibility
Created with R2010a
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

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

Start Hunting!