use matrix, premutation?

i use the command
c=[1,2,3,4,5] perms(c)
to generate all permutation of c. and there are 2 equation which use the
j=sin(0*c)+sin(72*c)+sin(144*c)+sin(216*c)+sin(288*c)
k=cos(0*c)+cos(72*c)+cos(144*c)+cos(216*c)+cos(288*c)
and Z=j+k
how can i find what permutation for minimum z? the permutation must be the same for both j and k at the same time ie
j=sin(0*1)+sin(72*2)+sin(144*3)+sin(216*4)+sin(288*5)
k=cos(0*1)+cos(72*2)+cos(144*3)+cos(216*4)+cos(288*5)
i actually wrote the euqation wrong, sorry. but if
j=sin(0)*c+sin(72)*c+sin(144)*c+sin(216)*c+sin(288)*c
k=cos(0)*c+cos(72)*c+cos(144)*c+cos(216)*c+cos(288)*c
would that change anything?

댓글 수: 1

Walter Roberson
Walter Roberson 2012년 4월 2일
Is your earlier question http://www.mathworks.com/matlabcentral/answers/34267-ordering-a-list-of-number considered answered? If so please Accept the answer; otherwise there is the appearance that this is an extension of the previous question that should be merged with it.

댓글을 달려면 로그인하십시오.

 채택된 답변

Thomas
Thomas 2012년 4월 2일

0 개 추천

Another way:
c=[1,2,3,4,5];
c=perms(c);
z=[];
j=[];
k=[];
for i=1:length(c)
j(i)=sin(0*c(i,1))*sin(72*c(i,2))+sin(144*c(i,3))+sin(216*c(i,4))+sin(288*c(i,5));
k(i)=cos(0*c(i,1))*cos(72*c(i,2))+cos(144*c(i,3))+cos(216*c(i,4))+cos(288*c(i,5));
z(i)=j(i)+k(i);
end
[p,q,r]=find(z==min(z));
c(q,:)
minz=min(z)

댓글 수: 6

kevin
kevin 2012년 4월 2일
can you explain step by step? like what does z=[]; do? and why is
j(i)=sin(0*c(i,1))*sin(72*c(i,2))+sin(144*c(i,3))+sin(216*c(i,4))+sin(288*c(i,5));
not
j(i)=sin(0*c(i,1))+sin(72*c(i,2))+sin(144*c(i,3))+sin(216*c(i,4))+sin(288*c(i,5)); or is that just typing error?
and what does
[p,q,r]=find(z==min(z)); do?
if i add anthor 2 equation in same form of j and k, what do i need to change?
c(q,:)
Thomas
Thomas 2012년 4월 2일
z=[]; etc preallocate the space for the variable,,
the j,k values calculate the particular values of j and k for the values of c (depending on i which is incremented in the for loop) c(i,1) gets the value of c for the current 1 and 1st column, c(i,2) same ith row second column etc.. If you add another 2 equation you can all l(i)=... , m(i)=... and if you are still adding all those to 'z' then z(i)=j(i)+k(i)+l(i)+m(i), and find the min values z and the index they are at.
FYI in matlab if do not understat a function or command you can type 'doc command' and it will show you the help for the comamnd.
'doc min' will give you the help for min function
kevin
kevin 2012년 4월 2일
ok thanks for the help, one last thing. what is this line?
[p,q,r]=find(z==min(z));
what is [p q r] ? do i need to change anything here when i add eq?
Thomas
Thomas 2012년 4월 2일
doc find
[row,col,v] = find(X, ...)
Matt Tearle
Matt Tearle 2012년 4월 2일
Actually, z = []; doesn't preallocate space. It only makes an empty matrix. A better approach would be
n = length(c);
j = zeros(n,1);
k = zeros(n,1);
for i = 1:n
...
Also, calculating z inside the loop isn't necessary. Just do z = j+k; at the end. Natural vectorized expressions like that are one of the main strengths of MATLAB. (Of course, I'd say that you don't need loops at all...)
Thomas
Thomas 2012년 4월 2일
true, z need not be calculated inside the loop.. z=j+k+.. at the end should work just as well..

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Matt Tearle
Matt Tearle 2012년 4월 2일

3 개 추천

I think this is what you're after:
c=[1,2,3,4,5];
cp = perms(c);
% 0*c1, 72*c2, 144*c3, 216*c4, 288*c5
allc = bsxfun(@times,[0 72 144 216 288],cp);
sc = sin(allc);
cc = cos(allc);
j = sc(:,1).*sc(:,2)+sum(sc(:,3:5),2);
k = cc(:,1) + cc(:,2).*cc(:,3) + cc(:,4) + cc(:,5);
z = j+k;
% which row of the c permutations corresponds to the minimum value of z?
cp(z==min(z),:)
If j and k were all sums, this would be a bit neater, but I'm assuming the products there are deliberate.
EDIT TO ADD: From your comment in reply to Thomas, it seems like maybe these should all be sums (ie no products). In that case:
c=[1,2,3,4,5];
cp = perms(c);
% 0*c1, 72*c2, 144*c3, 216*c4, 288*c5
allc = bsxfun(@times,[0 72 144 216 288],cp);
% sin(0*c1) + sin(72*c2) + ...
j = sum(sin(allc),2);
k = sum(cos(allc),2);
z = j+k;
% which row of the c permutations corresponds to the minimum value of z?
cp(z==min(z),:)

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

질문:

2012년 4월 2일

Community Treasure Hunt

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

Start Hunting!

Translated by