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 Center および File ExchangeLoops 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