Selecting cross values from two arrays

1 vue (au cours des 30 derniers jours)
SB
SB le 13 Sep 2019
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 commentaires
Raj
Raj le 13 Sep 2019
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 le 17 Sep 2019
Hi,
Can you explain what you want to do with these matrices, your doubt is not clear.

Connectez-vous pour commenter.

Réponses (2)

KALYAN ACHARJYA
KALYAN ACHARJYA le 17 Sep 2019
Modifié(e) : KALYAN ACHARJYA le 17 Sep 2019
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 le 17 Sep 2019
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(:)];

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Produits


Version

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by