Calculate mean of a matrix with different indices

조회 수: 1 (최근 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 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