loop do add easy part

1 次查看(过去 30 天)
how can I add this values together using the other for loop which is commented. It should function but it gives me some error.
for i =1:length(M)
%for j =1:6
MU=M(i,1)+M(i,2)+M(i,3)+M(i,4)+M(i,5)+M(i,6);
%MU=MU+MU(i,j); %tried this one as well but got error
%end
end
I want a final value for MU that is the sum of all numbers of have in each column of a matrix which exists of 10 row and six columns.
if for example this could work:
for i =1:length(M)
for j =1:6
MU=MU+M(i,j);
end
end
Is there any solutions to this?
*MU is
MU=char(0);
I will try to explain one more time from the start maybe I could explain it more clear
I have this matrix
M =
0 3.5304 8.7190 13.8538 16.9663 21.4803
0 3.4610 8.6222 13.8627 16.9212 21.3976
0 5.3416 8.6408 13.8793 18.6035 21.4253
0 5.4316 8.6749 13.8940 18.7298 21.4659
0 5.4576 10.6434 13.9081 18.7637 23.5175
0 5.4142 10.7144 13.9158 18.7278 23.5865
0 2.9547 8.2616 13.3956 16.3060 21.1298
0 2.9208 8.1767 13.4297 16.2556 21.0527
0 5.0420 8.1690 13.4130 17.8082 21.0228
0 5.1563 8.2055 13.4248 17.9452 21.1053
I want to add each column value together for example in MU, so I get one line at the end which looks like this:
MU = 0 sunOfColumn2 sunOfColumn3 sunOfColumn4 sunOfColumn5 sunOfColumn6
then I need to devide each of these values (which each is sum of my columns) by number of the integers I haved added together which is 10
Like MU/10
så I get an average, and I want these averages in MU
like:
MU = 0 averageOfColumn2 averageOfColumn3 averageOfColumn4 averageOfColumn5 averageOfColumn6
I hope I explained better what I am trying to do now
  3 个评论
Nicle Davidson
Nicle Davidson 2021-11-5
编辑:Nicle Davidson 2021-11-5
no I don't want sum everything together. Please see my additional explanation.
I need to add each column together and then get the average of it. I need all values of the average back in a matrix og what it could be called like:
MU = [averageOfColumn1 averageOfColumn2 averageOfColumn3 averageOfColumn4 averageOfColumn5 averageOfColumn6]
something like this
Stephen23
Stephen23 2021-11-5
编辑:Stephen23 2021-11-5
"by number of the integers I haved added together which is 10"
Your matrix does not contain any non-zero integers, so the number of such integers you can add together is zero.
"I need to add each column together and then get the average of it."
So why are you doing this very complex, indirect messing around with loops and that very odd CHAR(0) ?
If you need the mean of the columns in a matrix, that is what you should ask about.
Have you tried using MEAN?

请先登录,再进行评论。

采纳的回答

Cris LaPierre
Cris LaPierre 2021-11-5
I would do this
M = [0 3.5304 8.7190 13.8538 16.9663 21.4803
0 3.4610 8.6222 13.8627 16.9212 21.3976
0 5.3416 8.6408 13.8793 18.6035 21.4253
0 5.4316 8.6749 13.8940 18.7298 21.4659
0 5.4576 10.6434 13.9081 18.7637 23.5175
0 5.4142 10.7144 13.9158 18.7278 23.5865
0 2.9547 8.2616 13.3956 16.3060 21.1298
0 2.9208 8.1767 13.4297 16.2556 21.0527
0 5.0420 8.1690 13.4130 17.8082 21.0228
0 5.1563 8.2055 13.4248 17.9452 21.1053];
M = 10×6
0 3.5304 8.7190 13.8538 16.9663 21.4803 0 3.4610 8.6222 13.8627 16.9212 21.3976 0 5.3416 8.6408 13.8793 18.6035 21.4253 0 5.4316 8.6749 13.8940 18.7298 21.4659 0 5.4576 10.6434 13.9081 18.7637 23.5175 0 5.4142 10.7144 13.9158 18.7278 23.5865 0 2.9547 8.2616 13.3956 16.3060 21.1298 0 2.9208 8.1767 13.4297 16.2556 21.0527 0 5.0420 8.1690 13.4130 17.8082 21.0228 0 5.1563 8.2055 13.4248 17.9452 21.1053
% get the aveage
MU = mean(M,1)
MU = 1×6
0 4.4710 8.8827 13.6977 17.7027 21.7184
  4 个评论
Nicle Davidson
Nicle Davidson 2021-11-5
Thank you, it helped a lot
Cris LaPierre
Cris LaPierre 2021-11-5
The 1 tells MATLAB to compute the mean along the first dimentsion.
You do not have to initialize MU.
If you have not yet gone through it, you may find MATLAB Onramp a great getting-started resource.

请先登录,再进行评论。

更多回答(1 个)

Sulaymon Eshkabilov
编辑:Sulaymon Eshkabilov 2021-11-5
Cris already gave a very good answer to your question, BUT if you want to get it via loop only as an exercise. Here is how it can be done:
% [for end] Loop
Rows=10; Cols = 6;
Vmax = 13;
M = randi(Vmax, Rows, Cols);
MU1 = 0;
for i =1:length(M)
for j =1:size(M,2)
MU1=MU1+M(i,j);
end
end
fprintf('SUM with [for .. end] loop: %d \n', MU1)
SUM with [for .. end] loop: 409
% Efficient Way
MU2 = sum(M, 1:2);
fprintf('SUM with sum(): %d \n', MU2)
SUM with sum(): 409

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by