Selecting cross values from two arrays
조회 수: 1 (최근 30일)
이전 댓글 표시
I have three arrays namely, s = [0,1000,2000], d = [0,1000,2000] and J = [0,5000,8000], where, J = 3*d+2000. Now within a for loop, when s(1) and d(1) are selected, I want to select J(1). When s(2),d(1) is selected, I want to select J(3). When s(2), d(2) is selected, I want to select J(2). What would be a generic way of doing this instead of hard coding?
댓글 수: 2
Raj
2019년 9월 13일
Sorry but your question is not clear at all.
"when s(1) and d(1) are selected" - What do you mean by 'selected'? you want user to give selection inputs?
"I want to select J(1)" - You want J(1) to get computed here or you already have J matrix before 'for' loop and you want to select J(1) & do something with it?
Mahesh Taparia
2019년 9월 17일
Hi,
Can you explain what you want to do with these matrices, your doubt is not clear.
답변 (2개)
KALYAN ACHARJYA
2019년 9월 17일
편집: KALYAN ACHARJYA
2019년 9월 17일
Form the question, if you pattern the s,d, and J , it becomes
1 1 1
2 1 3
2 2 2
Have you find any pattern on the sequence? I dont think so...(one label condition)
Though you can do that by mutiple loops and condition statements, but direct implementation is much easier and faster here.
"What would be a generic way of doing this instead of hard coding?"
In addition, all vectors having 3 lengths only, you can do it by direct implementation, why do you think you have to go for "for loop" here.
Good Luck!
댓글 수: 0
Andrei Bobrov
2019년 9월 17일
s = [0,1000,2000];
d = [0,1000,2000];
J = [0,5000,8000];
ii = fullfact([3,3,3]);
out = [s(ii(:,1))',d(ii(:,2))',J(ii(:,3))'];
or
[ii,jj,k] = ndgrid(s,d,J);
out = [ii(:),jj(:),k(:)];
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!