Plug in matrix values into a Formula
이전 댓글 표시
Hi, can some one help me with the following:
I have matrix U and X
U=[-1.5 -1.0 -.5 0]; X = [1.5 0 0 0 0];
I want to substitute all the values of Big X and Big U into small x and u in following equation:
syms x u; X_E=x^2+*u
and i want my results to be in a matrix form. I was doing the following; however i want to find out a more efficient way to do it
X_Next=[subs(X_E,[x u],[X(1,1) U(1,1)]); subs(X_E,[x u],[X(1,1) U(1,2)]) ;subs(X_E,[x u],[X(1,1) U(1,3)]) ;subs(X_E,[x u],[X(1,1) U(1,4)])]
Thanks a lot in advance.
댓글 수: 3
dpb
2014년 5월 22일
X_E=X.^2+U; % unless I'm missing something don't need symbolics here
If want to change values of X and U and reevaluate, use anonymous function--
>> x=rand(2); % some values
>> u=randn(2);
>> f=@(X,U) X.^2+U; % the functional form
f =
@(X,U)X.^2+U
>> f(x,u)
ans =
0.0109 -0.4297
0.1324 1.7811
>> u=-u;
>> f(x,u)
ans =
0.8484 1.9189
1.0877 -0.5476
>>
Observe changing the value in the u-array and reevaluating changed the results of the function.
Fredy
2014년 5월 22일
채택된 답변
추가 답변 (3개)
...how i go about selecting the minima of every 5 rows...
OK, here is where you need to think (and it helps when you get to where you dream in :) ) Matlab--
>> min(reshape(J_COST,5,[])).'
ans =
1.5000
0.7500
0.2500
>>
I'll let you think on that a while... :) HINT: "memory storage order"
댓글 수: 7
Fredy
2014년 5월 27일
dpb
2014년 5월 27일
Of course...
>> [mn,imn]=min(reshape(J_COST,5,[]))
mn =
1.5000 0.7500 0.2500
imn =
4 4 3
>> u=[1 0.5 0 -0.5 -1];
>> u(imn)
ans =
-0.5000 -0.5000 0
>> u(imn)
It will venture to suggest you dig into the doc's more in detail looking at the optional returns and the details of the functions as you use them and then really think about whether one or more of those extras might just help...altho you have gotten quite a long way from the one exposition on anonymous functions we started of with, it seems, so kudos on that, and sometimes it's not so obvious.
Do you follow the primary "trick" here, though? It's important both for the immediate problem and in being a key to many Matlab solutions.
Fredy
2014년 5월 27일
dpb
2014년 5월 27일
The "trick" of using reshape to rearrange the vector into a 2D array over which to use the mean function...
Fredy
2014년 5월 27일
dpb
2014년 5월 27일
...reshaping J_COST by a new matrix that has J_COST minimun in every 5 rows,
Close, but not, precisely, no...read from the inside out and if needed look at the result of the intermediary steps to see what's happening. As mentioned before, the "how" is significant piece of understanding that will prove to be invaluable going forward in using Matlab. As said before, it's tied into internal storage order in Matlab which is "column major".
Fredy
2014년 5월 27일
dpb
2014년 5월 27일
interp1(T(:,1),T(:,2),Z,'nearest',NaN)
댓글 수: 20
Fredy
2014년 5월 28일
dpb
2014년 5월 28일
T(:,1) and T(:,2) have to be the numeric vector you showed--can't have any non-numeric stuff hanging around. "X" in the error is referring to the argument "X" in the documentation.
>> T
T =
1.5000 1.5000
1.0000 0.7500
0.5000 0.2500
>> Z
Z =
2.5000 2.0000 1.5000 1.0000 0.5000
>> interp1(T(:,1),T(:,2),Z,'nearest',nan)
ans =
NaN NaN 1.5000 0.7500 0.2500
>>
Fredy
2014년 5월 28일
Fredy
2014년 5월 28일
dpb
2014년 5월 28일
Look up "logical addressing"
Really need to just begin with "Getting Started" and work thru the examples of basic Matlab operations...
Which topic do you recommend me reading...
You can write the original question as a one-liner...altho I use a "helper function" for the purpose as "syntactic sugar". It is given as
function flg=iswithin(x,lo,hi)
% returns T for values within range of input
% SYNTAX:
% [log] = iswithin(x,lo,hi)
% returns T for x between lo and hi values, inclusive
flg= (x>=lo) & (x<=hi);
I'll leave you to ponder on how to use it... :)
NB: I have a "utilities" subdirectory that contains a bunch of these little snippets for a variety of such purposes. Akin to this one is isbetween that is identical except it uses a non-inclusive bounds. There's another version that's general that includes optional flag variables to allow for setting each limit individually as in- or ex-clusive.
NB: as you have written your find above, note that you have found the locations in a that you want to retain, not those that you wished to modify. How do you need to rearrange the logic test (or modify the present test result) to get the locations to change?
Also, the key point I'm trying to teach is that you don't need find here. Although it works to solve the problem for this type of exercise (which is a very common type) it's overkill.
Fredy
2014년 5월 29일
Yes, it's a user-written function -- like any other, put it in an m-file of the same name and invoke as any other as well. You can not put function definitions in a script file or at the command line. The m-file must be located in a directory on matlabpath to be found, of course.
Read up on the section on programming scripts and functions and the difference between...
ADDENDUM:
Then, once you've accomplished that, we're back to the previous point of "how to use it". It's still tied to the object lesson in logical addressing I've been harping on. The BIG HINT is that you do not need any temporary variables in this kind of operation; it can be done as a one-liner.
Again, reread the doc page I posted a link to a couple comments ago--it's got the detail. If that still doesn't get you home, post again and I'll then give out the secret...but I'd really feel like accomplished more if you worked it out on your own... :)
Fredy
2014년 5월 29일
Again, you're not taking the generalization to its logical (so to speak :) ) conclusion. I'm pretty sure there's an example there w/ no intermediaries, too, but I didn't go look right now...
Where I've been trying to get you to is something like...
C(C==0)=nan;
Your previous of a range--
C(~iswithin(C,14,18))=nan;
IOW, getting you to realize that unless you need the actual indices for some other reason, you don't need find and the result of a logical test is a logical array that is just as valid as an indexing expression as an intermediary variable storing that result...both, like the memory rearrangement we did some time ago to compute averages over even subsets of an array, are key points in effective use of Matlab.
Fredy
2014년 5월 30일
dpb
2014년 5월 30일
I'm sure it would be, but...
1) what are x and u and where do they come from; seem invariant here?
2) what is the final result and what's the quitting criterion?
Let's just have one complete set of sample inputs and desired output...looks to me like the end result should end up being derivable unless there's something I don't see...but, there's not a fully consistent single place that has all the inputs for a given case afaict. There's no A above, for example, so, let's just get a clean go at it. Also, are there restrictions on sizes--the hardcoded "5" in the reshape operation will fail if there's not a multiple so have to be some limits somewhere...
And, your example above defines a C 2D array then replaces it immediately w/
C=[interp1(A(:,1),A(:,2),Z,'nearest',NaN)];
D=[B+C];
Whassup w/ that??? In this case you don't need the original C and as noted, unless Z is 3x3, the result of C above won't be commensurate in size with B to make the addition possible and then you've got the problem once get past the that immediately following is the aforementioned problematic
[mn,imn]=min(reshape(D,5,[]));
which crashes in flames because mod(numel(D),5) ~=0
You need a lot more work in defining what it is you're actually trying to do; you can't have worked thru the above even manually to get a single iteration, what more the second or third.
Fredy
2014년 5월 31일
dpb
2014년 6월 1일
Not enough -- doesn't address any of the problems outlined above.
Give one COMPLETE set of input data and what you expect for output. what your input code looks like is immaterial; need a set of data from which you start and then what you expect as output.
Fredy
2014년 6월 1일
dpb
2014년 6월 1일
One more time...example data, results...
Fredy
2014년 6월 3일
Natalya Shedlovskiy
2022년 5월 10일
0 개 추천
how I can plug Major heed losses formula
카테고리
도움말 센터 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!