Calculate mean of a matrix with different indices

2 ビュー (過去 30 日間)
Dion Theunissen
Dion Theunissen 2021 年 3 月 17 日
コメント済み: Bjorn Gustavsson 2021 年 3 月 17 日
Hi,
I have a loop as below.
RCAr = []
for k = 1:length(RCA);
...
...
afstand = thisData(1:a,1);
lengte = size(dx);
lengte = round(lengte);
for i = 1:lengte-1
P1 = [dx(i), dy(i), dz(i)];
P2 = [dx(i+1), dy(i+1), dz(i+1)];
angleradian(i) = acos((P2(2)-P1(2)) / norm(P2-P1));
distance(i) = norm(P1 - P2);
end
angleradian2(:,1) = angleradian;
distance2(:,1) = distance;
RCAr(:,k) = angleradian2; %this point is not working
plot(afstand,angleradian2, '-')
hold on
end
I want to make a matrix of k columns with all the angleradians. But the indices of angleradian does not have the same length. How can I fix this?
I want to calculate the mean of each row and stdev of each row of that matrix.

採用された回答

Bjorn Gustavsson
Bjorn Gustavsson 2021 年 3 月 17 日
So you effectively have different number of dx for each k? If so I'd do this something like this:
angleradian2 = nan([MaxNrRows,MaxNrCols]); % pre-allocate a nan-array with correct size
RCAr = angleradia2; % just make place for this one too
distance = RCAr; % and this one,
afstand = RCAr; % and this one...
for i1 = 1:length(RCA); % Changed k to i1 to get identifying label on loop-variable
...
...
afstand(i1,1:a) = thisData(1:a,1);
lengte = size(dx,1); % or 2 instead of 1, size returns a 2 (at least) element array with
% [sz1, sz2, ... szN] since you use this as an upper
% bound of your iteration it should be a scalar!
lengte = round(lengte);
for i2 = 1:lengte-1 % changed i to i2 for same reason as above AND keep i free for complex unit
P1 = [dx(i2), dy(i2), dz(i2)];
P2 = [dx(i2+1), dy(i2+1), dz(i2+1)];
angleradian2(i1,i2) = acos((P2(2)-P1(2)) / norm(P2-P1));
distance(i1,i2) = norm(P1 - P2);
end
RCAr(i1,:) = angleradian2(i1,:); %this point is not working
plot(afstand,angleradian2, '-')
hold on
end
HTH
  4 件のコメント
Dion Theunissen
Dion Theunissen 2021 年 3 月 17 日
Allright, thanks for your help! It works for me.
Bjorn Gustavsson
Bjorn Gustavsson 2021 年 3 月 17 日
Great!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by