how to find a mean across rows

i have a code that produces a matrix, i want it to find the mean across the rows rather than down each column. i tried the below but keep getting an error.
??? In an assignment A(I) = B, the number of elements in B and
I must be the same.
lambdaMJ = 7.2;
MJ_Flow = (30/60)*2;
A = 100;
B = 70;
C = 4;
D = B + C;
E = A - B - C;
for a = 1:5;
Num_cylces = 1;
XMJ=(poissrnd(lambdaMJ, A, Num_cylces));
lambdaMJ_sec(a) = mean(XMJ)/10;
FMJ_cars_wait = @(T11,MJ_V11) (lambdaMJ_sec(a));
[T11,MJ_V11] = ode45(FMJ_cars_wait,[0,E],MJ_Flow);
Major11(a) = mean(MJ_V11,2); %CANT GET THIS TO WORK
end

 Accepted Answer

Image Analyst
Image Analyst on 4 Oct 2013
Edited: Image Analyst on 5 Oct 2013
What is the loop for? It's not needed. Just do this:
Major11 = mean(MJ_V11,2);
If MJ_V11 changes inside the loop, then you can still have it in the loop:
for a = 1 : 5 % No() or ; needed
MJ_V11 = whatever.....
Major11(a) = mean(MJ_V11(a,:),2);
end
Note how I added an index to Major11 so that it doesn't get overwritten every time.

8 Comments

harley
harley on 4 Oct 2013
Edited: harley on 4 Oct 2013
thanks, its part of a large body of coding within a loop, when i try the above i get the error. edited code above to give you more info on my issue.
??? In an assignment A(I) = B, the number of elements in B and
I must be the same.
If you're doing
Major11(a) = mean(MJ_V11,2); %CANT GET THIS TO WORK
then that won't work because a is a single index and you're trying to stuff a whole vector of a bunch of values into the single element. That won't work unless you make Major11 a cell array instead of a regular numerical matrix. What do you plan on doing with this vector of row means (where the row mean is calculated going across columns)? Why are you trying to stuff it into a single element?
This is a small part of a larger body of code, its going be used to plot against some other coding that is not shown here, i just need the mean of each row to do so. Any help would be appreciated.
Major11(a) = mean(MJ_V11,2);
gives a column vector of the mean of all rows. If you want the mean of one particular row , say you're in a loop over rows, then you can do
Major11(a) = mean(MJ_V11(row, :), 2);
thanks for your time, when i try the below in the loop
Major11(a) = mean(MJ_V11,2);
i keep getting.
??? In an assignment A(I) = B, the number of elements in B and
I must be the same.
Image Analyst
Image Analyst on 5 Oct 2013
Edited: Image Analyst on 5 Oct 2013
Yes, I know, and I've told you 3 times that you're trying to stick a bunch of numbers into an array meant for just one number. But why didn't you try the code I gave you to fix it instead of continuing to use code that doesn't work? Is there some reason why you don't want to try the code I suggested?
harley
harley on 5 Oct 2013
Edited: harley on 5 Oct 2013
thanks again, i tried the below but it just gives me the first 5 values of the first column/ loop.
Major11(a) = mean(MJ_V11(a,:),2);
That's because you had
for a = 1:5
If you want all the rows, you need to go over all the rows, not just 5 of them:
[rows, columns] = size(MJ_V11);
for a = 1 : rows

Sign in to comment.

More Answers (0)

Categories

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

Tags

Community Treasure Hunt

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

Start Hunting!