Calculate mean of a matrix with different indices

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

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

Thanks, I only need to know the maxnrows and columns before looping. Is this also possible during the loop? Or do I have to check this before and add this in this row.
Preferably you should figure out how big your variables need to be beforehand - that reduces the number of times matlab needs to reallocate space for the arrays (this is the terrible time-waster in many "lazy" algorithms that relies on the automatic array-growing capabilities of matlab (it costs to be lazy (unfortunately...))). The general idea to use nan for your data is that you can use the 'omitnan' flag to the statistics functions afterwards to only take the average of the numerical values in the arrays, if you grow stuff automagically there will be zero-padding done implicitly.
Another way around this problem is to use another array, lets say nrSamples, to store how many data-points you get for each i2.
Another way of dealing with this is to store the results for each i2 in cell-arrays and then calculate the statistics cell-by-cell.
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!