loop do add easy part
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Nicle Davidson
el 5 de Nov. de 2021
Comentada: Cris LaPierre
el 5 de Nov. de 2021
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 comentarios
Stephen23
el 5 de Nov. de 2021
Editada: Stephen23
el 5 de Nov. de 2021
"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?
Respuesta aceptada
Cris LaPierre
el 5 de Nov. de 2021
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];
% get the aveage
MU = mean(M,1)
4 comentarios
Cris LaPierre
el 5 de Nov. de 2021
The 1 tells MATLAB to compute the mean along the first dimentsion.
You do not have to initialize MU.
Más respuestas (1)
Sulaymon Eshkabilov
el 5 de Nov. de 2021
Editada: Sulaymon Eshkabilov
el 5 de Nov. de 2021
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)
% Efficient Way
MU2 = sum(M, 1:2);
fprintf('SUM with sum(): %d \n', MU2)
0 comentarios
Ver también
Categorías
Más información sobre Loops and Conditional Statements en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!