How do I compute the mean value of odd rows of a random matrix (mxm)? (using if and rem)

5 visualizzazioni (ultimi 30 giorni)
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 Commenti
dpb
dpb il 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)?
JP Retaw
JP Retaw il 1 Mar 2021
In fact, they are. Before that I got do a random Matrix mxm using (if), then compute the mean value for all the columns using 'for loop'. And now they are asking for that.

Accedi per commentare.

Risposta accettata

Jan
Jan il 1 Mar 2021
Modificato: Jan il 1 Mar 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.

Più risposte (0)

Categorie

Scopri di più su Creating and Concatenating Matrices in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by