Info
此问题已关闭。 请重新打开它进行编辑或回答。
if i have y4='0.2+6' is there a way to make p=0.2+6 but keeping the numbers like that and not equaling it to 6.2.
1 次查看(过去 30 天)
显示 更早的评论
if i have y4='0.2+6' is there a way to make p=0.2+6 but keeping the numbers like that and not equaling it to 6.2.
14 个评论
Elliott Cameron
2020-4-27
so y4='0.2+6' with the function inside ''. However i want to have p=0.2+6 without the '' but also i do not want p=6.2, i want p=0.2+6 staying like that and not adding together
Ameer Hamza
2020-4-27
Elliot, what about Walter's answer here: https://www.mathworks.com/matlabcentral/answers/520415-hello-if-i-have-two-equations-1-2x-2-and-2-3x-2-4x-7-is-there-a-way-to-remove-the-x-in-a-new-equat. Didn't it pointed you to the right direction?
Tommy
2020-4-28
Can you explain a bit further? Seems to me that multiplying something by (0.2) + 6 is the same thing as multiplying by 6.2. Are you hoping for the two terms to remain separate when displayed to the user?
Elliott Cameron
2020-4-28
multiplying something first by 0.2 and then adding 6 is not the same as multiplying by 6,2? no walters answer still had the equation within '....'
Rik
2020-4-28
This only makes sense in a linear concatenation of char arrays. What exact situation are you trying to use this in? I don't see how this will likely result in valid Matlab syntax. I also don't understand how this would be valid mathematics.
Elliott Cameron
2020-4-28
this is my demand equation demand=-0.2*x + 20 and later on i am using this find (RandData(1,:).*(-0.2)+20>RandData where this is multiplied by the slope of the line and then the intercect is added. If it were to be multiplied by 19.8 then the answer is different
Rik
2020-4-28
Why not leave it as a function?
demand_eq=@(x) -0.2*x+20;
result=demand_eq(RandData(1,:));
result=result>RandData;
This is much easier to understand than this.
demand_eq='(-0.2)+20';
result=zeros(1,size(RandData,2));
for n=1:numel(result)
result(n)=eval(sprintf('%.2f*%s',RandData(1,n),demand_eq))
end
result=result>RandData;
You see the train wreck that results in? You even need eval to get anything at all. You lose any option for vectorization and optimization. You also can't use other Matlab functions that accept functions as inputs.
Tommy
2020-4-28
Ah I did not interpret 'multiply something by (0.2) + 6' as 'multiply something by (0.2) and then add 6'. I agree with Rik. I don't think you should store this as a character vector in the first place. If for whatever reason you have to start with a character vector, I would first parse it to pull out the numbers and then go from there.
Steven Lord
2020-4-28
If the function to be evaluated is always going to be a polynomial, another option would be to store it as a vector of coefficients and use polyval to evalute it.
p = [-0.2, 20];
x = 0:0.25:5;
y = polyval(p, x)
One potential benefit this has over the anonymous function is that you could manipulate or query the coefficients by indexing into p.
David Goodmanson
2020-4-28
Hi Elliott,
could you just do something like
x = 6.2
xintdec = [floor(x) x-floor(x)]
xintdec = 0.2000 6.0000
y = xintdec*rand
y = 0.1265 3.7942
此问题已关闭。
回答(0 个)
此问题已关闭。
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)