I don't understand what the following sum function does.
mA=zeros(M,1);
mA(1)=sum(cA(1,:))*dx;
mA is a column vector, so what does mA(1) mean?
The documentation for the sum function indicates that taking the sum of a vector just results in one value which is the sum of all of the values in the vector. Is that also what happens when you take the sum of a single row in a matrix (sum(cA(1,:)))? So is mA(1) equal to a single value? I can't seem to find out what mA(1) means for a vector.

 Respuesta aceptada

Walter Roberson
Walter Roberson el 8 de Jun. de 2021

0 votos

mA(1) in this context is designating a single array element as the destination for the assignment. You are putting the results into the first box of the stack of cubby-holes.
Is that also what happens when you take the sum of a single row in a matrix (sum(cA(1,:)))
Yes.

4 comentarios

Robert Demyanovich
Robert Demyanovich el 8 de Jun. de 2021
So because mA is a vector, Matlab allows the cell designated as 1,1 to be referred to by simply the row number, in this case. And the "results" from the sum function in this case is a single value.
Walter Roberson
Walter Roberson el 8 de Jun. de 2021
Not precisely. When you use a single subscript for an array, MATLAB uses "linear indexing". The locations of the entire array can be accessed using a single index that is the relative location in memory from the beginning of the array. Locations are numbered going down columns, so A11 A21 A31 and after the end of the first column continue with the beginning of the second column A12 A22 A32 and so on.
Suppose you have a column vector, then A11 would be index 1, A21 would be index 2, A31 would be index 3. If it were a row vector then A11 would be index 1, A12 would be index 2, A13 would be index 3. Either way, the "first" entry is at index 1, the "second" at 2, and so on.
Robert Demyanovich
Robert Demyanovich el 8 de Jun. de 2021
I see. So in a 5x5 array, the first cell would have an index number of 1 and the last cell would have an index number of 25. Thanks for the explanation.
Right.
reshape(1:25,5,5)
ans = 5×5
1 6 11 16 21 2 7 12 17 22 3 8 13 18 23 4 9 14 19 24 5 10 15 20 25
reshape() changes the information about dimensions without changing the order in memory, so the values you see above in this particular case also happen to correspond to linear index.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by