Converting product into individual entries

3 ビュー (過去 30 日間)
Mahendra Yadav
Mahendra Yadav 2025 年 1 月 20 日
コメント済み: Mahendra Yadav 2025 年 1 月 21 日
Can I write [4*3 5 7*0 11 3*9] in the following format.
[3,3,3,3,5,0,0,0,0,0,0,0,11,9,9,9]
  3 件のコメント
Mahendra Yadav
Mahendra Yadav 2025 年 1 月 20 日
For any product, Let's say we have a*b.
Then "a" refers here to the number of times, the value "b" should appear.
Hence for a*b, we have
[b,b,b,b,b,b.....(a times)]
Stephen23
Stephen23 2025 年 1 月 20 日
@Mahendra Yadav: Sure, I know what product is. But nowhere do you explain how were are supposed to determine which factors you want to use. For example, given the product 12 you could have:
  • a=3 b=4
  • a=4 b=3
  • a=2 b=6
  • a=6 b=2
  • a=1 b=12
  • a=12 b=1
So far you have given absolutely no explanation of how you want to select the specific factors given some random value.
Or perhaps you are just not explaining the form that your data actually have. I cannot guess this.

サインインしてコメントする。

採用された回答

Steven Lord
Steven Lord 2025 年 1 月 20 日
If you have the individual terms and their replication factors already, see the repelem function. If you don't you may need to use the factor function first.
  2 件のコメント
Mahendra Yadav
Mahendra Yadav 2025 年 1 月 20 日
Yes, I'm aware about both of these functions. But how can I implement these in the above mentioned array.
Or
wheter any alternative solutions are possible?
Steven Lord
Steven Lord 2025 年 1 月 20 日
Do you have your data in this form?
option1 = [4*3 5 7*0 11 3*9]
option1 = 1×5
12 5 0 11 27
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Or do you have data in this form?
option2a = [4 1 7 1 3]
option2a = 1×5
4 1 7 1 3
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
option2b = [3 5 0 11 9]
option2b = 1×5
3 5 0 11 9
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
If you have option 1, how do you handle the ambiguity with 7*0 called out by Stephen23?
And since you seem to be trying to do something with prime factorizations, why is the first group [3 3 3 3] and not [2 2 2 2 2 2] (which you would write 6*2)? Both those groups sum to 12 which is the first element of option1, so how do you disambiguate? And why isn't the third group just empty [] (zero 7s) instead of [0 0 0 0 0 0 0] (seven 0s)?
With option 2:
x = repelem(option2b, option2a)
x = 1×16
3 3 3 3 5 0 0 0 0 0 0 0 11 9 9 9
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

サインインしてコメントする。

その他の回答 (1 件)

Xiaotao
Xiaotao 2025 年 1 月 20 日
a = [3*ones(1,4), 5, zeros(1,7), 11, 9*ones(1,3)]
a = 1×16
3 3 3 3 5 0 0 0 0 0 0 0 11 9 9 9
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
  4 件のコメント
Stephen23
Stephen23 2025 年 1 月 21 日
The simpler MATLAB approach:
cnt = [4,1,7,1,3]; % counts
val = [3,5,0,11,9]; % values
out = repelem(val,cnt)
out = 1×16
3 3 3 3 5 0 0 0 0 0 0 0 11 9 9 9
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Mahendra Yadav
Mahendra Yadav 2025 年 1 月 21 日
Thank You!
It worked

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

製品


リリース

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by