How can I create a vector
显示 更早的评论
I want to create a vector b from the vector a, such as:
a = [2 4 3]
b = [1/2 1/2 1/4 1/4 1/4 1/4 1/3 1/3 1/3]
(note that there are as many fractions in b, as the sum of the vector a. Also, the number of different fractions correspond with the individual values of a)
Is there any easy way to generalize this example?
Thanks
2 个评论
Javier Imaz Higuera
2022-8-23
Matt J
2022-8-23
You should be getting Code Analyzer messages discouraging you from growing b iteratively in the loop.
回答(1 个)
a=[2,4,3];
b=1./repelem(a,a)
类别
在 帮助中心 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!