I would like to know why I am obtaining as a result in this code, a matrix (15x1) and not a number. I am computing a test statistics equation that involves matrices products, so I am multiplying this matrix sizes:
((1x15)*(15x15)*(15x1))X((1x15)*(15x15)*(15x1))^2
I guess I am supposed to achieve a number, from this matrices products, but as I have said, MATLAB returns me as a solution of this equation a 15x1 matrix. Otherwise, at the end I have put the command size(T), to know the matrix size of T, and it returns me that is a 1x1 matrix (a number). Does anybody know why this two things doesn't match?
Following, here is my MATLAB code attached if you want to take a look at it

댓글 수: 1

Image Analyst
Image Analyst 2014년 8월 22일
The attached code also requires .xlsx workbooks, which were not attached. Attach the workbooks if you want us to try the code.

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

 채택된 답변

Star Strider
Star Strider 2014년 8월 22일

2 개 추천

It is difficult to read your code and I can’t run it. I ran a simulation of the matrix operation you describe, and got a scalar result as expected.
I suggest you check the sizes of the matrices in your equation. One of your vectors may actually be a matrix.

댓글 수: 9

Josep
Josep 2014년 8월 22일
I have forgotten you don't have the excel files, so obviously, you are not able to run it.
I don't understand what you mean, saying the one of my vectors may actually be a matrix. All my values operating in the equation are matrices.
Here i attach you what I receive from MATLAB in a PDF file
Star Strider
Star Strider 2014년 8월 22일
If the vector in the .pdf is T, then something is wrong. The size should be (15x1), not (1x1).
Again, please post the actual line of your code that is doing that calculation, and the sizes of all the vectors and matrices that are in it.
Okay, here is the code that is doing the calculation:
for j=1:Nby,
Fk(:,j)
T = inv(Fk(:,j)'*inv(V)*Fk(:,j))*(Fk(:,j)'*inv(V)*r)^2;
size(T)
end
Matrix sizes:
Fk(15x1)
V(15x15)
r(15x1)
The lines of code you just posted would give the result you wrote in your PDF. You're asking matlab to print Fk(:, j) which is indeed 15x1 and checking the size of T which is 1x1. Everything is as expected.
Change your lines to
for j = 1:Nby
T = inv(Fk(:,j)'*inv(V)*Fk(:,j))*(Fk(:,j)'*inv(V)*r)^2;
disp(T); %better to be explicit that you want this printed, than playing around with semi-colons
end
I don’t understand why that’s not producing scalar values for T.
However, if you intend to save the Nby individual values for T, you have to index them. Since the individual matrix operation terms both produce scalar values, divide the second term by the first rather than using inv on the first term:
for j=1:Nby,
Fk(:,j)
T(j) = ((Fk(:,j)'*inv(V)*r)^2)/(Fk(:,j)'*inv(V)*Fk(:,j));
size(T(j))
end
Josep
Josep 2014년 8월 22일
Guillaume, that works now. As you said, I needed to ask matlab to print T, not Fk. Sorry for this mistake, I am new on Matlab.
Star Strider, why putting this / ?
Do yo get the same results? Why are you always trying to avoid inv?
Star Strider
Star Strider 2014년 8월 22일
It makes the code simpler and faster to divide scalars using ‘/’ than inv.
I avoid inv where possible because the ‘\’ operator is a much more efficient and computationally stable method of multiplying by inv, although sometimes it is unavoidable. Here, inv on the first term (a scalar) is the the same as 1/(Fk(:,j)'*inv(V)*Fk(:,j)), so it makes sense to compute it as a scalar.
Josep
Josep 2014년 8월 22일
Nice!
Thanks Star Strider one more time
Star Strider
Star Strider 2014년 8월 22일
My pleasure!

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2014년 8월 22일

댓글:

2014년 8월 22일

Community Treasure Hunt

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

Start Hunting!

Translated by