Calculate mean of a matrix with different indices

2 次查看(过去 30 天)
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 个评论

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops 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