필터 지우기
필터 지우기

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

조회 수: 3 (최근 30일)
Redwan
Redwan 2023년 12월 4일
댓글: Redwan 2023년 12월 4일
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에서 Surface and Mesh Plots에 대해 자세히 알아보기

제품


릴리스

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by