フィルターのクリア

Assigned solve outputs to a matrix via for loops

1 回表示 (過去 30 日間)
Stephen Pasek
Stephen Pasek 2017 年 3 月 24 日
回答済み: Walter Roberson 2017 年 3 月 29 日
Hello, I am having a problem where I am attempting to solve an equation inside of a for loop and assign its result to the respective matrix cell. It has inputs from a matrix of the same size as it should be outputting to, yet I still get the error "Assignment has more non-singleton rhs dimensions than non-singleton subscripts". Here is my relevant code:
for i=1:s % i is steps of t
for j = 1:s % j is steps of r
Ac(i,j) = 2*pi*rp(j)*t(i);
if Ac(i,1)<An
Acrit(i,j) = Ac(i,1); % For distances less than .025mm
else
Acrit(i,j) = An; % For everything else
end
Aratio(i,j) = Ac(i,j)/Acrit(i,j);
end
end
for i=1:s
for j=1:s
M = sym('M');
macheqn(i,j) = 1/M*(2/(k+1)*(1+(k-1)/2*M^2))^((k+1)/(2*(k-1)));
assume(M,'real');
Mach(i,j)=double(solve(macheqn(i,j)==Aratio(i,j),M));
end
end
  2 件のコメント
Darshan Ramakant Bhat
Darshan Ramakant Bhat 2017 年 3 月 28 日
What is 'An' is it a matrix or a constant? It would be better if you give input data as well
Walter Roberson
Walter Roberson 2017 年 3 月 28 日
Should we assume that k is a scalar?
Is there a reason to assign the same value to all elements of macheqn?
Your macheqn is not going to have an analytic solution so use vpasolve instead of solve.
Careful: the macheqn can have two solutions.

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

回答 (2 件)

Nanda Gupta
Nanda Gupta 2017 年 3 月 29 日
I understand that you want to solve an equation iteratively inside the for loop and assign its answer each time in to a respective matrix cell.
This error is a result of incorrect indexing of multidimensional arrays. Check out this documentation for indexing the multidimensional arrays: http://www.mathworks.com/help/matlab/math/multidimensional-arrays.html;jsessionid=b58f142b9ecaa292bc0adfa73247#f1-86846

Walter Roberson
Walter Roberson 2017 年 3 月 29 日
It looks likely to me that your solve() is returning an empty expression. double() of that is empty, and you would then be trying to store empty into a location of size 1.
We would need the values of the other variables to test this ourselves.
Try using vpasolve() instead of solve()

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by