Error using horzcat Dimensions of arrays being concatenated are not consistent.
3 ビュー (過去 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
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 件のコメント
回答 (1 件)
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 Center および File Exchange で Surface and Mesh Plots についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!