how make matrix from a for loop
Show older comments
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
Accepted Answer
More Answers (1)
Sara
on 9 Jun 2014
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);
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!