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
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