Converting product into individual entries
Mostra commenti meno recenti
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 Commenti
Given this vector:
V = [4*3,5,7*0,11,3*9]
how do you determine that specific vector? For example, there are infinite possible products that give 0, not just 7*0:
Why do you expect 0,0,0,0,0,0,0 and not 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 which has exactly the same product?
Why do you expect 3,3,3,3 and not 4,4,4 ? Or 6,6 ? Or 2,2,2,2,2,2 ? Or 1,1,1,1,1,1,1,1,1,1,1,1 ?
Why do you expect 9,9,9 and not 3,3,3,3,3,3,3,3,3 ?
So far you have not provided any explanation of how you selected those particular factors from vector V.
Mahendra Yadav
il 20 Gen 2025
Stephen23
il 20 Gen 2025
@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.
Risposta accettata
Più risposte (1)
a = [3*ones(1,4), 5, zeros(1,7), 11, 9*ones(1,3)]
4 Commenti
Mahendra Yadav
il 21 Gen 2025
cnt=[4 1 7 1 3]; % counts
val=[3 5 0 11 9]; % values
a = arrayfun(@(c, v) v * ones(1, c), cnt, val, 'UniformOutput', false);
a = [a{:}] % Concatenate the results
The simpler MATLAB approach:
cnt = [4,1,7,1,3]; % counts
val = [3,5,0,11,9]; % values
out = repelem(val,cnt)
Mahendra Yadav
il 21 Gen 2025
Categorie
Scopri di più su Logical in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!