Error using horzcat Dimensions of arrays being concatenated are not consistent.

2 次查看(过去 30 天)
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
Error using horzcat
Dimensions of arrays being concatenated are not consistent.
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.

回答(1 个)

the cyclist
the cyclist 2023-12-4
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);

类别

Help CenterFile Exchange 中查找有关 Rubik's Cube 的更多信息

产品


版本

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by