how make matrix from a for loop
이전 댓글 표시
My code like this,
........... ............
x_error=zeros(1,1);
y_error=zeros(1,1);
z_error=zeros(1,1);
lz=1.5; %lx=1.35; %ly=1.4;
for i=1:1:5 ly=i;
for j=1:1:5;
lx=j;
......................
X=abs(x(1,1));
Y=abs(x(1,2));
H=abs(x(1,3));
x_error(i,j)=abs(X-lx);
y_error(i,j)=abs(Y-ly);
z_error(i,j)=abs(abs(z1-H)-lz);
end
end
x_er=x_error
y_er=y_error
z_er=z_error
it's work but when i change i=1:1:5 to i=1:0.1:5 and j=1:1:5; to j=1:0.1:5;
then error like this >>Attempted to access x_error(1.1,1); index must be a positive integer or logical.
how can i solve this i need to get the x_error value in matrix form not in a cell array form
채택된 답변
추가 답변 (1개)
Sara
2014년 6월 9일
First, when you allocate variables before the for loop, you need to allocate them so that they can contain all the elements. This means that for the code you have posted, x_error = zeros(5,5) and so on. Second, doing loop where the increment is not an integer is not recommendable for numerical reasons. So, create an array before the for loop with the elements you need to evaluate:
myarray_x = 1:0.1:5;
Then use:
for i = 1:numel(myarray_x)
and inside this loop do:
lx = myarray_x(i);
카테고리
도움말 센터 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!