how to use a 'loop' for certain counts
1 ビュー (過去 30 日間)
表示 古いコメント
I have 15 areas in my power data if i plot. want to calculate each area by using trapz function. can calculate each area manually(is shown in the code), but want to find these area automatically. maybe loop can be the solution.
there are 16 zero crossing indices. so from 1:16, there are 15 areas. used this below written loop, but it's showing the final value. I want to get every area from 1 to 16.
i=0;
for i=1:length(zeroaxes)
time(i:zeroaxes(i))=time(i:zeroaxes(i));
power(i:zeroaxes(i),1)=power(i:zeroaxes(i));
areaone=trapz(time,power);
end
would you please let me know any way ? attached the mat file.
thank you very much
Code(area calculated manually):
A=load('power.mat');
power=A.power;
time=1:length(power);
zci = @(v) find(v(:).*circshift(v(:), [-1 0]) <= 0); % Returns Zero-Crossing Indices
zeroaxes = zci(power);
%% Area Calculation
time1=time(1:zeroaxes(2)); % time for area1
power1=power(1:zeroaxes(2)); % time for area1
area1=trapz(time1,power1); % integrated area1
time2=time(zeroaxes(2):zeroaxes(3));
power2=power(zeroaxes(2):zeroaxes(3));
area2=trapz(time2,power2);
time3=time(zeroaxes(3):zeroaxes(4));
power3=power(zeroaxes(3):zeroaxes(4));
area3=trapz(time3,power3);
time4=time(zeroaxes(4):zeroaxes(5));
power4=power(zeroaxes(4):zeroaxes(5));
area4=trapz(time4,power4);
time5=time(zeroaxes(5):zeroaxes(6));
power5=power(zeroaxes(5):zeroaxes(6));
area5=trapz(time5,power5);
time6=time(zeroaxes(6):zeroaxes(7));
power6=power(zeroaxes(6):zeroaxes(7));
area6=trapz(time6,power6);
time7=time(zeroaxes(7):zeroaxes(8));
power7=power(zeroaxes(7):zeroaxes(8));
area7=trapz(time7,power7);
time8=time(zeroaxes(8):zeroaxes(9));
power8=power(zeroaxes(8):zeroaxes(9));
area8=trapz(time8,power8);
time9=time(zeroaxes(9):zeroaxes(10));
power9=power(zeroaxes(9):zeroaxes(10));
area9=trapz(time9,power9);
time10=time(zeroaxes(10):zeroaxes(11));
power10=power(zeroaxes(10):zeroaxes(11));
area10=trapz(time10,power10);
time11=time(zeroaxes(11):zeroaxes(12));
power11=power(zeroaxes(11):zeroaxes(12));
area11=trapz(time11,power11);
time12=time(zeroaxes(12):zeroaxes(13));
power12=power(zeroaxes(12):zeroaxes(13));
area12=trapz(time12,power12);
time13=time(zeroaxes(13):zeroaxes(14));
power13=power(zeroaxes(13):zeroaxes(14));
area13=trapz(time13,power13);
time14=time(zeroaxes(14):zeroaxes(15));
power14=power(zeroaxes(14):zeroaxes(15));
area14=trapz(time14,power14);
time15=time(zeroaxes(15):zeroaxes(16));
power15=power(zeroaxes(15):zeroaxes(16));
area15=trapz(time15,power15);
0 件のコメント
採用された回答
Rik
2021 年 12 月 14 日
編集済み: Rik
2021 年 12 月 14 日
You were attempting to overwrite elements of your array.
You were also trying to create numbered variables, instead of using arrays. This is an instinct many new users have, and every time it should be discouraged. It forces you to use eval, which is a very effective way to write slow buggy code.
A=load(websave('power.mat','https://www.mathworks.com/matlabcentral/answers/uploaded_files/833630/power.mat'));
power=A.power;
time=1:length(power);
zci = @(v) find(v(:).*circshift(v(:), [-1 0]) <= 0); % Returns Zero-Crossing Indices
zerosaxes = zci(power);
zerosaxes(1)=1;
%% Area Calculation
area=zeros(1,(numel(zerosaxes)-1));
for n=1:(numel(zerosaxes)-1)
time_current=time(zerosaxes(n):zerosaxes(n+1));
power_current=power(zerosaxes(n):zerosaxes(n+1));
area(n)=trapz(time_current,power_current); % integrated area
end
disp(area)
If you want to store each iteration, you should use a cell array, which you can index with curly braces.
その他の回答 (1 件)
Karthik P.V
2021 年 12 月 14 日
Can you try with this for loop?
for i=1:length(zeroaxes)
try
eval(['time_',num2str(i),'=time(i:zeroaxes(i))']);
eval(['power_',num2str(i),'=power(i:zeroaxes(i))']);
power(i:zeroaxes(i),1)=power(i:zeroaxes(i));
eval(['area_',num2str(i),'=trapz(time_',num2str(i),',power_',num2str(i),')']);
catch
continue;
end
end
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!