Index problem help?
1 次查看(过去 30 天)
显示 更早的评论
JL555
2015-9-22
for i=1:n
for j=1:n
Pl=P(i)*B(i)(j)*P(j);
seems like i'm getting error with writing B(i)(j)...How will i code it in another way?
采纳的回答
Guillaume
2015-9-22
编辑:Guillaume
2015-9-22
Possibly, you meant to write
Pl(i,j) = P(i)*B(i,j)*P(j)
Or possibly, you meant something else entirely. It's impossible to tell with so little information.
Most likely also, the loops are not needed and it can all be done with matrix operations.
17 个评论
Walter Roberson
2015-9-22
You vectorized your code and you did not show us the new code. We can't tell you what needs to be fixed.
JL555
2015-9-22
Pi=x(1:m,1:n1);
for i=1:n
for j=1:n
if pmin<Pi;
pmin=Pi;
else if Pi<pmax;
Pi=pmax;
C=Pi(i)*B(i,j)*Pi(j);
C1=B0(i)*Pi(i);
Pl=C+C1+Boo;
Pi=Pd+Pl;
end
end
end
end
Walter Roberson
2015-9-22
That code will not generate the error message you indicate.
Why did you not follow the conversation in http://www.mathworks.com/matlabcentral/answers/241944-using-summation-and-modulus
Note that you are doing a test of one vector compared to another. You would get a matrix dimensions must agree error there but not an inner dimensions must agree error.
If you were to switch so the two vectors are the same orientation then you face the problem that the test
if A < B
means the same as
if all(A < B)
and is only true if the comparison holds for every item in the vector.
JL555
2015-9-22
Actually the link to that answer is not valid anymore something terribly wrong with that program and i have a new one.And yes i'm getting a matrix dimension must agree ...so it is not possible to use the 'lt' to generate a matrix of 1x6 or should i compare with only one elemnt of that matrix?
Walter Roberson
2015-9-22
It is obvious to me from the code that you should be rewriting using matrix algebra. But every time I fix your code for you, you change it completely and look like you have not understood anything I wrote. You also do not answer questions about what size of output you are expecting from each step. It leaves me reluctant to assist.
JL555
2015-9-22
编辑:Walter Roberson
2015-9-22
function [Ft Pi Pl]=FPAeld(x)
global objfun data pmin pmax Pd B B0 Boo
n=length(data(:,1));
[m n1]=size(x);
Pi=x(1:m,1:n1);
for i=1:n
for j=1:n
if data(1,4)<Pi;
data(1,4)=Pi;
else if Pi<data(1,5);
Pi=data(1,5);
C=Pi(i)*B(i,j)*Pi(j);
C1=B0(i)*Pi(i);
Pl=C+C1+Boo;
Pi=Pd+Pl;
end
end
end
end
a=data(:,1);
b=data(:,2);
c=data(:,3);
e=data(:,6);
f=data(:,7);
for i=1:n,
Pi=[Pi Pi(i)];
Ft=a(i)+b(i)*Pi(i)+c(i)*Pi(i).^2+abs(e(i)*sin(f(i)*(pmin-Pi(i))));
end
ok here the new code .....i'm feeding 'x' from an algorithm..its a 6x1 matrix. What i need in the end is Pi value which will have 6 values and i'll feed that value to the Ft formula in the end
Walter Roberson
2015-9-22
Pi is a vector but if the if comparing it to data(1,4) holds then you assign data(1,4) to replace ALL of the vector.
JL555
2015-9-22
ok last question...'x' out there like i said is a 6x1 matrix..in the 5th line my Pi should be a 6x1 matrix too right? but i'm getting a single value ..why?
JL555
2015-9-22
Oh and one thing ,i have the pseudo code of an algorithm ...any tutorials of how i can write that pseudo code into a matlab program..
Walter Roberson
2015-9-22
I explained several days ago why
Pi=[Pi Pi(i)];
is going to just end up copying the same element over and over again. You are ignoring everything I write about why your program is not working.
JL555
2015-9-22
ok sorry dude i've been working with this program for one month now i managed to eliminate all the errors but everything is wrong with the programming...anyway are u familiar with economic load dispatch??
Walter Roberson
2015-9-22
I am not familiar with economic load dispatch.
Note: I am not "dude". Or "man".
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
标签
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 (한국어)