필터 지우기
필터 지우기

How can I create a vector

조회 수: 1 (최근 30일)
Javier Imaz Higuera
Javier Imaz Higuera 2022년 8월 23일
댓글: Matt J 2022년 8월 23일
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
Javier Imaz Higuera 2022년 8월 23일
found a way:
b=[];
for i = 1:length(a)
b=[b,a(i).*ones(a(i),1)']
end
b=b.^(-1);
Matt J
Matt J 2022년 8월 23일
You should be getting Code Analyzer messages discouraging you from growing b iteratively in the loop.

댓글을 달려면 로그인하십시오.

답변 (1개)

Matt J
Matt J 2022년 8월 23일
편집: Matt J 2022년 8월 23일
a=[2,4,3];
b=1./repelem(a,a)
b = 1×9
0.5000 0.5000 0.2500 0.2500 0.2500 0.2500 0.3333 0.3333 0.3333

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by