Assigned solve outputs to a matrix via for loops
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Darshan Ramakant Bhat
am 28 Mär. 2017
What is 'An' is it a matrix or a constant? It would be better if you give input data as well
Walter Roberson
am 28 Mär. 2017
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.
Antworten (2)
Nanda Gupta
am 29 Mär. 2017
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
0 Kommentare
Walter Roberson
am 29 Mär. 2017
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()
0 Kommentare
Siehe auch
Kategorien
Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!