How to create a loop that runs through the rows of a table?
조회 수: 7 (최근 30일)
이전 댓글 표시
I am trying to create a loop that runs through a table (2016 x 2) that takes the data from each column and inputs it into an equation and plots each result for each row. This is what I have attempted so far however, I am struggling to get this to work. Any help would be greatly appreciated.
rows = height(T);
for row = 1:rows
a1=T(row,1);
a2=T(row,2);
P=a1*a2;
end
hold on
plot(p,row)
댓글 수: 0
채택된 답변
Cris LaPierre
2021년 3월 16일
편집: Cris LaPierre
2021년 3월 16일
You don't need a for loop for this.
P=T{:,1}.*T{:,2};
plot(P)
댓글 수: 3
Cris LaPierre
2021년 3월 16일
You need to capture the result from each loop. Right now, each loop overwrites the previous value with the current value. You want to keep them all.
T=readtable("T.xlsx")
E1=236;
E2=5;
G12=2.6;
v12=0.25;
v21=(v12*E2)/E1;
t=0.25;
N1=[1.5;3;0];
Q=[(E1/(1-v12*v21)) ((v12*E2)/(1-v12*v21)) 0; ((v21*E1)/(1-v12*v21)) (E2/(1-v12*v21)) 0; 0 0 G12];
rows = height( T);
Z1=0;
Z2=t;
Z3=2*t;
Z4=3*t;
Z5=4*t;
for row = 1:1:rows
a1=T{row,1};
a2=T{row,2};
%Matricies definition
T1=[cosd(a1)^2, sind(a1)^2, -2*cosd(a1)*sind(a1); sind(a1)^2, cosd(a1)^2, 2*cosd(a1)*sind(a1); cosd(a1)*sind(a1), -cosd(a1)*sind(a1), (cosd(a1)^2-sind(a1)^2)];
T2=[cosd(a2)^2, sind(a2)^2, -2*cosd(a2)*sind(a2); sind(a2)^2, cosd(a2)^2, 2*cosd(a2)*sind(a2); cosd(a2)*sind(a2), -cosd(a2)*sind(a2), (cosd(a2)^2-sind(a2)^2)];
Q1=T1*Q*transpose(T1);
Q2=T2*Q*transpose(T2);
A=(Q1*(Z2-Z1))+(Q2*(Z3-Z2))+(Q1*(Z4-Z3))+(Q2*(Z5-Z4));
B=(0.5*Q1*((Z2^2)-(Z1^2)))+(0.5*Q2*((Z3^2)-(Z2^2))+(0.5*Q1*(Z4^2)-(Z3^2)))+(0.5*Q2*((Z5^2)-(Z4^2)));
D=((1/3)*Q1*((Z2^3)-(Z1^3)))+((1/3)*Q2*((Z3^3)-(Z2^3))+((1/3)*Q1*(Z4^3)-(Z3^3)))+((1/3)*Q2*((Z5^3)-(Z4^3)));
e0=(A^-1)*N1;
e1=(transpose(T1))*e0;
e2=(transpose(T2))*e0;
s1=Q*e1;
s2=Q*e2;
sx1(row)=s1(1,1);
sy1(row)=s1(2,1);
end
plot(sx1,sy1,'.')
추가 답변 (1개)
ANKUR KUMAR
2021년 3월 16일
편집: ANKUR KUMAR
2021년 3월 16일
clc
clear
A=randi(20,25,5); %generating random data
T = array2table(A);
equation= @(x) x*5 + log(x) % creating an equation
names=T.Properties.VariableNames;
for kk=1:length(names)
values=T(:,names{kk});
plot(equation(values{:,:}))
hold on
end
댓글 수: 0
참고 항목
카테고리
Help Center 및 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!