MATLAB Answers

# How to write this matlab code?

조회 수: 3(최근 30일)
Ali Nouri 2020년 3월 9일
댓글: Benjamin Großmann 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 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 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 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 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표시숨기기 이전 댓글 수: 1
Benjamin Großmann 2020년 3월 9일
Yes, thanks for the advice. A.'*A is for the non-conjugate transpose

댓글을 달려면 로그인하십시오.

### Community Treasure Hunt

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

Start Hunting!

Translated by