Selecting cross values from two arrays

조회 수: 1 (최근 30일)
SB
SB 2019년 9월 13일
답변: Andrei Bobrov 2019년 9월 17일
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
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
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
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!

Andrei Bobrov
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(:)];

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품


릴리스

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by