Effacer les filtres
Effacer les filtres

Info

Cette question est clôturée. Rouvrir pour modifier ou répondre.

Hi, I have a three questions about loops. The question I attempted was 'Create normal random data for 10 rows and 5 columns. Compute the means and standard deviations of each row by using a loop. Display the results for each row.'

1 vue (au cours des 30 derniers jours)
Sara  Renmiu
Sara Renmiu le 16 Mai 2020
Clôturé : MATLAB Answer Bot le 20 Août 2021
The answer is
Create normal random data for 10 rows and 5 columns.
Compute the means and standard deviations of each row by using a loop.
Display the results for each row.
%}
data = randn([10 5])
for i = 1:size(data,1)
m = mean(data(i,:),2);
s = std (data(i,:),[],2);
disp('Row data:')
disp(m)
disp(s)
end
My questions are
1)What does size mean in this context (for i)?
2)What does mean(data(i, : ), 2); mean?
3)What does std (data(i, : ), [] 2); mean? And why are the [] square brackets used here?
  1 commentaire
Mehmed Saad
Mehmed Saad le 16 Mai 2020
Modifié(e) : Mehmed Saad le 16 Mai 2020
The best way of understanding that is
  1. Open MATLAB
  2. On the top right corner in MATLAB app, there's a search documentation field. Type size in it. it will show you array size, read that document
  3. After that, read Array Indexing, mean and std by following step 2
  4. There are examples given in each document, read those examples for better understanding
  5. Also you can get help on any MATLAB command by typing help size (or whatever command you want to see)
help size
size Size of array.
D = size(X), for M-by-N matrix X, returns the two-element row vector
D = [M,N] containing the number of rows and columns in the matrix.
For N-D arrays, size(X) returns a 1-by-N vector of dimension lengths.
Trailing singleton dimensions are ignored.
.
.
.
See also length, ndims, numel.
Reference page for size
Other functions named size

Réponses (1)

Walter Roberson
Walter Roberson le 16 Mai 2020
  1. size(data,1) is number of rows
  2. mean(something, 2) is mean along the columns. The number of columns will be reduced to 1, with the other dimensions remaining the same as they were.
  3. std() along the columns. The [] exist to occupy an argument so that MATLAB can tell the difference between giving information about which normalization scheme to use (normalize by N-1 or normalize by N), versus giving information about which dimension to act along.

Cette question est clôturée.

Community Treasure Hunt

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

Start Hunting!

Translated by