Error in array bounds

1 次查看(过去 30 天)
Midlij
Midlij 2021-10-13
评论: DGM 2021-10-13
clc; clear all; close all;
load('bunny.mat')
%% Visualize PC
figure;
scatter3(P(1,:), P(2,:), P(3,:),10,Color,'filled');
xlabel('x axis');
ylabel('y axis');
zlabel('z axis');
%% Problem 1:
title('bunny1');
saveas(gcf, "bunny1");
for sx=0.01:5
P = [];
s = [sx, 0, 0;
0, sx, 0;
0, 0, sx];
for i = 1:2396
pi = P(:,i);
spi = s * pi;
P = [SP,spi];
end
figure;
scatter3(P(1,:), P(2,:), P(3,:),10,Color,'filled');
xlabel('x axis');
ylabel('y axis');
zlabel('z axis');
end
Below is not part of the code
*color is 1x2396 and P is 3x2396
Why do I get the error "Index in position 2 exceeds array bounds.
Error in script (line 23)
pi = P(:,i);"? and how do I fix it?

回答(2 个)

Walter Roberson
Walter Roberson 2021-10-13
"P is 3x2396"
No it is not. You assign [] to P.
  1 个评论
Midlij
Midlij 2021-10-13
let's suppose that it is 3x2396. How do I fix the error?

请先登录,再进行评论。


DGM
DGM 2021-10-13
编辑:DGM 2021-10-13
% ...
% this loop has been simplified to the point that it works
% for sx = 0.01:5
% s = [sx, 0, 0;
% 0, sx, 0;
% 0, 0, sx];
%
% for k = 1:size(P,2)
% P(:,k) = s * P(:,k);
% end
% end
% but it's needlessly roundabout, because it simplifies to
P = P*4.01;
% which is probably not what you want, but you haven't described that.
scatter3(P(1,:), P(2,:), P(3,:),10,Color,'filled');
xlabel('x axis');
ylabel('y axis');
zlabel('z axis');
  2 个评论
Midlij
Midlij 2021-10-13
I tried the code, but it is not providing me with the required values when the figures are displayed. What could have gone wrong?
DGM
DGM 2021-10-13
Nobody knows, because nobody else knows what the required values are.
The only information you provided was nonfunctioning code which appears to do what I demonstrated. You're multiplying each point by the identity matrix (multiplied by a scalar sx). As the name "identity matrix" implies, the result is the point itself multiplied by sx. Since the results are overwritten each time the loop iterates, the result is P multiplied by the last value of sx, which is 4.01. You could plot the points each time...
% ...
for sx = 0.01:5
Ps = P*sx;
scatter3(Ps(1,:), Ps(2,:), Ps(3,:),10,Color,'filled'); hold on
end
xlabel('x axis');
ylabel('y axis');
zlabel('z axis');
If the code is supposed to do something else, then you'll have to clearly describe what that is.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Axis Labels 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by