フィルターのクリア

Use element as indices to continue calculation

1 回表示 (過去 30 日間)
Anni Shi
Anni Shi 2022 年 10 月 28 日
コメント済み: Anni Shi 2022 年 11 月 1 日
Hello, I split a set of data (3600 rows) to four segments and want to do separate calculation for each of them.
I have identified the start indices (t values) of each segment and combined them to a vector:
ind=[1,764,1335,1459,2151]
I want to calculate the mean values of x, y and z in each segment.
t=data(:,1);
P=data(:,2:4);
x=P(:,1); y=P(:,2);z=P(:,3);
I really appreciate your suggestions. Thank everyone in advance!

採用された回答

CHIRANJIT DAS
CHIRANJIT DAS 2022 年 10 月 29 日
編集済み: CHIRANJIT DAS 2022 年 11 月 1 日
Not sure what you are looking for. Hope the below code serves your requirement.
Data=csvread('data.csv'); ind=[1,764,1335,1459,2151];
t=Data(:,1);
P=Data(:,2:4); x=P(:,1); y=P(:,2);z=P(:,3);
xsec=[]; ysec=[]; zsec=[];
for i=1:length(ind)-1
disp(i)
xs1=nanmean(x(ind(i):ind(i+1))); xsec=[xsec;xs1];
ys1=nanmean(y(ind(i):ind(i+1))); ysec=[ysec;ys1];
zs1=nanmean(z(ind(i):ind(i+1))); zsec=[zsec;zs1];
end
final=[xsec,ysec,zsec]; % 1st,2nd,3rd columns are means of individual ind sections of x,y,z
Cheers.
  5 件のコメント
Torsten
Torsten 2022 年 11 月 1 日
Elements in the ind-vector are counted twice in your code.
Should be
xmean(i) = mean(x(ind(i):(ind(i+1)-1)));
ymean(i) = mean(y(ind(i):(ind(i+1)-1)));
zmean(i) = mean(z(ind(i):(ind(i+1)-1)));
instead of
xs1=nanmean(x(ind(i):ind(i+1))); xsec=[xsec;xs1];
ys1=nanmean(y(ind(i):ind(i+1))); ysec=[ysec;ys1];
zs1=nanmean(z(ind(i):ind(i+1))); zsec=[zsec;zs1];
Anni Shi
Anni Shi 2022 年 11 月 1 日
@Torsten Thank you for checking. I tested his version and it works fine. I didn't really notice it get counted twice in the result.

サインインしてコメントする。

その他の回答 (1 件)

Torsten
Torsten 2022 年 10 月 29 日
data = readmatrix("https://de.mathworks.com/matlabcentral/answers/uploaded_files/1172778/data.csv");
ind=[1,764,1335,1459,2151];
t=data(:,1);
P=data(:,2:4);
x=P(:,1); y=P(:,2);z=P(:,3);
ind = [ind,numel(t)+1];
for i = 1:numel(ind)-1
xmean(i) = mean(x(ind(i):(ind(i+1)-1)));
ymean(i) = mean(y(ind(i):(ind(i+1)-1)));
zmean(i) = mean(z(ind(i):(ind(i+1)-1)));
end
xmean
xmean = 1×5
1.0e+03 * 2.1501 2.2066 2.2559 2.2694 2.1244
ymean
ymean = 1×5
1.0e+03 * 3.6214 3.5568 3.4870 3.4679 3.6411
zmean
zmean = 1×5
-111.2237 -207.9818 -269.9069 -301.0967 -33.5058
  3 件のコメント
Torsten
Torsten 2022 年 11 月 1 日
I thought you might also be interested in the mean for the elements from 2152 to the end ...
Anni Shi
Anni Shi 2022 年 11 月 1 日
Got it! Thank you so much!

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeDirect Search についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by