How to write this matlab code?

2 ビュー (過去 30 日間)
Ali Nouri
Ali Nouri 2020 年 3 月 9 日
Hi
I have the below equation and I want to solve by matlab, but i am little bit confused how to write.
A=(B-mean(B))*P
C=A^2
B is matrix of 8760X30 and P is vector of 30X1.
do i need to use for loop to solve it or how will you write it??
Thanks in advance

採用された回答

John D'Errico
John D'Errico 2020 年 3 月 9 日
編集済み: John D'Errico 2020 年 3 月 9 日
By the way, learn to use semi-colons.
What happens here in MATLAB?
A=(B-mean(B))*P;
In B, MATLAB sees a matrix of size 8760X30. mean(B) computes the mean of all rows, which is a vector of size 1X30. It subtracts that vector from each row of B. (This is true only if you are using release R2016b or later of MATLAB. Earlier releases will fail if you write that.)
Then it multiplies the difference by the column vector of length 30 in P, a matrix*vector product. You might call it a dot product. So the result is a column vector, of length 8760.
Is that what you should be computing? If so, then MATLAB does exactly what you wanted, at least in that line. Of course, you may have intended something completely different. I cannot know what you really want to do, only what you write.
Next, I have no certainty what you intend by the line
C = A^2
Well, i can guess. Are you looking to square each element of A, putting the new vector into C? If so, then you need to write it as
C = A.^2;
Thus an element-wise squaring of the elements of A.

その他の回答 (2 件)

ME
ME 2020 年 3 月 9 日
Assuming you want the mean of the whole matrix (not mean by row/column or something else) then the first line will be:
A = (B - mean(B(:)))*P;
which will give an 8760x1 array. You can then do your second line as:
C = A.^2;
assuming you want each element squared - which I guess must be what you want because otherwise the dimensions don't work out.
  1 件のコメント
Benjamin Großmann
Benjamin Großmann 2020 年 3 月 9 日
Since R2018b there is an option for that
mean(B, 'all')
This is also available for min() and max() (and maybe for others).

サインインしてコメントする。


Benjamin Großmann
Benjamin Großmann 2020 年 3 月 9 日
In Matlab, you could write something like
B = rand(8760, 30); % matrix B of size 8760x30 with random values
P = rand(30, 1); % vector P of size 30,1 with random values
A =(B-mean(B))*P;
BUT: Matlab computes the mean of each column of B resulting in a row vector. Then this row vector is substracted from the matrix B, where we do have a problem from a mathematical perspective. Here, matlab replicates the row vector "mean(B)" 8760 times to fit the dimensions of B.
You could also pass the option 'all' to the mean function to get the mean of all matrix elements, then the result is a scalar and the dimensionaity does fit.
Either way, your output from the first equation A is a column vector 8760x1. This being said, the second equation also gives a problem from a mathematical perspective. To square all elements of this vector you could use the dot operator C=A.^2 or, if you intend to calculate the scalar product, you can write C=A'*A; which transposes the vector before multiplication with itself.
  2 件のコメント
John D'Errico
John D'Errico 2020 年 3 月 9 日
Be careful with recommending A'*A. If the elements of A are real numbers, then the result is simply the sum of squares. However, if the elements of A might be cpomplex, then it is NOT just the sum of squares of elements. the ' operator is a CONJUGATE transpose.
Benjamin Großmann
Benjamin Großmann 2020 年 3 月 9 日
Yes, thanks for the advice. A.'*A is for the non-conjugate transpose

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by