How do I compute the mean value of odd rows of a random matrix (mxm)? (using if and rem)
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
First of all I already created a random matrix mxm using 'if', then compute the mean value for each columnn using 'for loop'. But now I got compute the mean value for odd rows using 'if' again, and the homework says 'use rem' as a hint.
Thanks in advance!
2 Kommentare
dpb
am 28 Feb. 2021
Sounds too suspiciously like homework...if so, we need to see whatyou've done on your own...and what the specific groundrules really are (are, in fact the above two mandated to be part of the solution)?
Akzeptierte Antwort
Jan
am 1 Mär. 2021
Bearbeitet: Jan
am 1 Mär. 2021
I do not have any idea, how "if" can be used to produce a random matrix. But using REM() to get the odd indices is easy:
for k = 1:m
if rem(k, 2) == 1
meanValue = mean(X(k, :));
% Now it is computed. POerhaps you want to store it in
% an output?
end
end
By the way, the matlabish way would be to omit the loops
meanVlaue = mean(X(1:2:end, :), 2)
Do you see the elegance? Your homework question asks you explicitly to use Matlab in an unefficient way. Therefore I've posted a solution, although it solves your homework.
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!