Calculate mean of a matrix with different indices

3 views (last 30 days)
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.

Accepted Answer

Bjorn Gustavsson
Bjorn Gustavsson on 17 Mar 2021
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 Comments
Dion Theunissen
Dion Theunissen on 17 Mar 2021
Allright, thanks for your help! It works for me.

Sign in to comment.

More Answers (0)

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!