how to store each for loop iteration output in a row of a matrix and then eventually get the full matrix for all iterations?

18 views (last 30 days)
hello, I have the variables ax,ay,az and r that for each iteration they give different output and should be stored in a atrix and eventually we need each row of that matrix to describe one iteration from the loop, I keep getting the error that says 'Error in receiver_location3 (line 45) H(i)=[ax ay az 1];
so baisiclly we need to find the ax,ay,az and r store them in a row vector and then somhow store that row vector from each iteration in a bigger matrix contaning all the iterations output. so anyone has any idea how to do that?
I attach below the code I am working on, there might be some lines in the code that are not related but just to make the bigger picture clear
for S=1:length(data3)
if(data3(S,6)>15)
newdata(i,:)=data3(S,:);
i=i+1;
end
end
while N<length(newdata)
for r=1:length(newdata)
if(SecOfDay==newdata(r,1))
counter_1=counter_1+1;
end
xs=newdata(N:counter_1,3);
ys=newdata(N:counter_1,4);
zs=newdata(N:counter_1,5);
dist=newdata(N:counter_1,6);
end
Fun = @(u) [sqrt((xs-u(1)).^2 + (ys-u(2)).^2 + (zs-u(3)).^2) - (c*u(4)) - (dist)];
x0 = [0;0;0;0];
u = lsqnonlin(Fun,x0);
SecOfDay=SecOfDay+30;
X=u(1);
Y=u(2);
Z=u(3);
e=u(4);
r= sqrt((xs-X).^2-(ys-Y).^2-(zs-Z).^2);
ax= (xs-X)/r;
ay=(ys-Y)/r;
az=(zs-Z)/r;
H(i)=[ax ay az 1];
N=counter_1+1;
for j=1:length(newdata)
H_j=[ax(j) ay(j) az(j) 1];
end
  1 Comment
Aakash Deep Chhonkar
Aakash Deep Chhonkar on 19 Jul 2021
It is hard to guess the issue because the code snippet is incomplete. I guess the issue is with var 'i', try using separate var to store the row vector in H.

Sign in to comment.

Answers (3)

Markus
Markus on 19 Jul 2021
I put your code in the code environment to get a clearer view:
for S=1:length(data3)
if(data3(S,6)>15)
newdata(i,:)=data3(S,:);
i=i+1; % i= previously defined? - now prev. i+amount of true if-cases
end
end
while N<length(newdata)
for r=1:length(newdata)
if(SecOfDay==newdata(r,1))
counter_1=counter_1+1;
end
xs=newdata(N:counter_1,3);
ys=newdata(N:counter_1,4);
zs=newdata(N:counter_1,5);
dist=newdata(N:counter_1,6);
end
Fun = @(u) [sqrt((xs-u(1)).^2 + (ys-u(2)).^2 + (zs-u(3)).^2) - (c*u(4)) - (dist)];
x0 = [0;0;0;0];
u = lsqnonlin(Fun,x0);
SecOfDay=SecOfDay+30;
X=u(1);
Y=u(2);
Z=u(3);
e=u(4);
r= sqrt((xs-X).^2-(ys-Y).^2-(zs-Z).^2);
ax= (xs-X)/r;
ay=(ys-Y)/r;
az=(zs-Z)/r;
H(i)=[ax ay az 1]; % i is now at the value from the first for-loop
You are working with i from the for-loop from above. I am not certain if that is what you really want. Maybe H(N) gives you what you want?
N=counter_1+1;
for j=1:length(newdata)
H_j=[ax(j) ay(j) az(j) 1];
end

Steven Lord
Steven Lord on 19 Jul 2021
Try adapting this example that creates the 5-by-5 multiplication table to your needs. I've left off the semicolon inside the loop so it displays the intermediate results. [I know there's a shorter way to create the multiplication table, but this is to demonstrate the technique.]
x = 1:5;
multTable = zeros(5);
for y = 1:5
z = y*x;
multTable(y, :) = z
end
multTable = 5×5
1 2 3 4 5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
multTable = 5×5
1 2 3 4 5 2 4 6 8 10 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
multTable = 5×5
1 2 3 4 5 2 4 6 8 10 3 6 9 12 15 0 0 0 0 0 0 0 0 0 0
multTable = 5×5
1 2 3 4 5 2 4 6 8 10 3 6 9 12 15 4 8 12 16 20 0 0 0 0 0
multTable = 5×5
1 2 3 4 5 2 4 6 8 10 3 6 9 12 15 4 8 12 16 20 5 10 15 20 25

Walter Roberson
Walter Roberson on 19 Jul 2021
Replace
for j=1:length(newdata)
H_j=[ax(j) ay(j) az(j) 1];
end
with
H_j = [ax(:), ay(:), az(:), ones(numel(ax),1)];

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!