RMS of matrices in a cell or array

5 views (last 30 days)
Mike
Mike on 15 Mar 2016
Commented: Mike on 17 Mar 2016
Hi all,
If I have an n x n matrix, P, I find the rms by RMS_P = sqrt(mean(Prv(:).^2));.
Please, how can I find the rms of the ith elements in an array, P(;,:,i), along the ith dimension? Thanks.
This is what I have that outputs the rms of a matrix:
ang = 0:90:270;
low_lim = ang - 1;
up_lim = ang + 1;
%The input, inp_file is a 1001 x 1001 matrix, and it's attached.
for i = 1:length(ang)
p_rot = zeros([1001 1001 4]);
ang_var = low_lim + (up_lim - low_lim).*rand(1,length(ang));
p_rot(:,:,i) = imrotate(inp_file,ang_var(i),'crop');
mean_p_rot = mean(p_rot,3);
P = (p_rot(:,:,1)) - mean_p_rot;
P(isnan(P)==1)=0;
RMS_P = sqrt(mean(P(:).^2));
end
I'm trying to repeat this loop multiple times. What I tried is:
for ii = 1: 5
for i = 1:length(ang)
p_rot = zeros([1001 1001 4]);
ang_var = low_lim + (up_lim - low_lim).*rand(1,length(ang));
p_rot(:,:,i) = imrotate(inp_file,ang_var(i),'crop');
mean_p_rot = mean(p_rot,3);
P = (p_rot(:,:,1)) - mean_p_rot;
P(isnan(P)==1)=0;
RMS_P = sqrt(mean(P(:).^2));
end
P_new(:,:,ii) = P;
RMS_P_new(ii) = sqrt(mean(P_new(:).^2));
end
I'm having trouble putting the first loop in another loop, and finding the rms of each ii in P_new(:,:,ii).
Please help. Thanks!
  4 Comments
dpb
dpb on 16 Mar 2016
Hmmm....oh, I'd forgotten rms is in Signal Processing toolbox. Try
sqrt(mean(x.*x))
instead or if x can be imaginary
sqrt(mean(x .* conj(x)))
Think should be noticeably faster than .^2.
But, that aside, does rms(P(:)) solve the issue however you compute it?
Mike
Mike on 17 Mar 2016
Thanks! I stored each rms result from the inner loop, as an array element in the second loop.
end
P_new(:,:,ii) = P;
RMS_P_new(:,:,ii) = RMS_P;
end
I want click "accept your answer" but there's no button for that. I think it's because you replied as a comment, not as an answer. Thanks dpb.

Sign in to comment.

Answers (1)

John BG
John BG on 17 Mar 2016
Mike
have you noticed that rms has an option to aim on the dimension you want to sweep along?
P=randi(10,3,3,3)
P(:,:,1) =
2.00 8.00 1.00
5.00 10.00 9.00
10.00 7.00 10.00
P(:,:,2) =
7.00 4.00 8.00
8.00 7.00 1.00
8.00 2.00 3.00
P(:,:,3) =
1.00 7.00 1.00
1.00 4.00 5.00
9.00 10.00 4.00
rms(P,1)
ans(:,:,1) =
6.56 8.43 7.79
ans(:,:,2) =
7.68 4.80 4.97
ans(:,:,3) =
5.26 7.42 3.74
rms(P,2)
ans(:,:,1) =
4.80
8.29
9.11
ans(:,:,2) =
6.56
6.16
5.07
ans(:,:,3) =
4.12
3.74
8.10
rms(P,3)
ans =
4.24 6.56 4.69
5.48 7.42 5.97
9.04 7.14 6.45
If you find this answer of any help solving your question, please click on the thumbs-up vote link,
thanks in advance
John
  1 Comment
Mike
Mike on 17 Mar 2016
Thanks John. That'd have worked if I could use the built-in rms function. I get an error message when I try that. I stored each rms result from the inner loop, as an array element in the second loop.
I clicked the thumbs up...thanks!
end
P_new(:,:,ii) = P;
RMS_P_new(:,:,ii) = RMS_P;
end

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!