Error using horzcat Dimensions of arrays being concatenated are not consistent.
2 views (last 30 days)
Show older comments
I am trying to type a code in matlab which should represent the 2x2 rubiks cube but i keep on getting an error and whatever i do or search i cant seem to correct it.
this is the code:
A = [1 2; 3 4];
A(:,:,2) = [5 6; 7 8];
B = cat(3,A,[9 10; 11 12]);
B(:,:,4) = [13 14; 15 16];
B(:,:,5) = [17 18; 19 20];
B(:,:,6) = [21 22; 23 24];
PocketCube = B;
figure;
facesColor = {[1 0.5 0], [1 1 1], [1 0 0], [1 1 0], [0 0 1], [0 1 0]};
subdivisions = 2;
for i = 1:6
faceColor = facesColor{i};
[X, Y] = meshgrid(linspace(0, 1, subdivisions + 1));
Z = PocketCube(:, :, i);
vertices = [X(:), Y(:), Z(:)];
faces = reshape(1:numel(vertices)/3, [], size(X, 2));
patch('Vertices', vertices, 'Faces', faces, ...
'FaceColor', faceColor, 'EdgeColor', 'k', 'LineWidth', 1);
hold on;
grid on;
end
axis equal;
axis off;
view(3);
Error using horzcat
Dimensions of arrays being concatenated are not consistent.
please point out the correction needed and any other issues if found to give the intended rubiks cube representation.
2 Comments
Answers (1)
the cyclist
on 4 Dec 2023
Your code will run to completion if you change the linspace command to have one fewer points.
(I did not check to see if this is a sensible result.)
A = [1 2; 3 4];
A(:,:,2) = [5 6; 7 8];
B = cat(3,A,[9 10; 11 12]);
B(:,:,4) = [13 14; 15 16];
B(:,:,5) = [17 18; 19 20];
B(:,:,6) = [21 22; 23 24];
PocketCube = B;
figure;
facesColor = {[1 0.5 0], [1 1 1], [1 0 0], [1 1 0], [0 0 1], [0 1 0]};
subdivisions = 2;
for i = 1:6
faceColor = facesColor{i};
[X, Y] = meshgrid(linspace(0, 1, subdivisions));
Z = PocketCube(:, :, i);
vertices = [X(:), Y(:), Z(:)];
faces = reshape(1:numel(vertices)/3, [], size(X, 2));
patch('Vertices', vertices, 'Faces', faces, ...
'FaceColor', faceColor, 'EdgeColor', 'k', 'LineWidth', 1);
hold on;
grid on;
end
axis equal;
axis off;
view(3);
See Also
Categories
Find more on Rubik's Cube in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!