How to store data of FOR LOOP iteration?

In the following code i want all the data of each iteration to be stored in P_new, and also B_xq, B_xq , B_xq repectively, but what i get is only the last one. What needs to be changed in my code?
k = convhull(data_coord);
for i = 1:length(k)
v1_x = x_data(k(i,1));
v1_y = y_data(k(i,1));
v1_z = z_data(k(i,1));
v2_x = x_data(k(i,2));
v2_y = y_data(k(i,2));
v2_z = z_data(k(i,2));
v3_x = x_data(k(i,3));
v3_y = y_data(k(i,3));
v3_z = z_data(k(i,3));
p1 = [v1_x v1_y v1_z];
p2 = [v2_x v2_y v2_z];
p3 = [v3_x v3_y v3_z];
ps = [p1; p2; p3];
p12 = p2-p1;
p23 = p3-p2;
P = [p12; p23];
q = sqrt(rand(5, 1));
q = [q q.*rand(5, 1)];
P_new = q*P+p1;
F_Bx = scatteredInterpolant(data_coord, B_x,'nearest');
B_xq = F_Bx(P_new(:,1),P_new(:,2),P_new(:,3));
F_By = scatteredInterpolant(data_coord, B_y,'nearest');
B_yq = F_By(P_new(:,1),P_new(:,2),P_new(:,3));
F_Bz = scatteredInterpolant(data_coord, B_z,'nearest');
B_zq = F_Bz(P_new(:,1),P_new(:,2),P_new(:,3));
end

 채택된 답변

Sudhakar Shinde
Sudhakar Shinde 2020년 10월 27일

0 개 추천

You can use P_new(i) or P_new{i} to store loop result.

댓글 수: 6

Nima
Nima 2020년 10월 27일
in case of P_new{i}, there is an error : Unable to perform assignment because brace indexing is not supported for variables of this type.
in case of P_new(i) : Unable to perform assignment because the left and right sides have a different number of elements.
From above code looks like need to use:
P_new(i,:)
%or
P_new(:,i)
Nima
Nima 2020년 10월 27일
another error : Unable to perform assignment because the indices on the left side are not compatible with the size of the right side.
Sudhakar Shinde
Sudhakar Shinde 2020년 10월 27일
Can you attach detailed inputs:
data_coord, x_data, y_data & z_data to reproduce issue.
data = csvread('20points3D.csv',5,0);
B_x = transpose(data([61:80]));
B_y = transpose(data([81:100]));
B_z = transpose(data([101:120]));
x_data = transpose(data([1:20]));
y_data = transpose(data([21:40]));
z_data = transpose(data([41:60]));
data_coord = [x_data,y_data,z_data];
Sudhakar Shinde
Sudhakar Shinde 2020년 10월 27일
P_new is required for calculations, so use below syntax to store outcomes:
B_xq(:,i)
B_yq(:,i)
B_zq(:,i)
As above final outcome is your area of interest P_new need not to be saved in loop and use as it is written now.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

릴리스

R2020a

질문:

2020년 10월 27일

댓글:

2020년 10월 27일

Community Treasure Hunt

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

Start Hunting!

Translated by