creating vector from n value

Hi everyone, how can I created 5 diffrent vectors based on:
n = [2 4 8 16 32]
if n = 2, N = [1/2 1/2]
if n=4, N = [1/4 1/4 1/4 1/4]
so on with 8 16 and 32
Please help me

2 件のコメント

Torsten
Torsten 2022 年 2 月 15 日
編集済み: Torsten 2022 年 2 月 15 日
for i=1:numel(n)
N{i} = 1/n(i)*ones(1,n(i));
end
Tu Nguyen
Tu Nguyen 2022 年 2 月 15 日
Thank you

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

回答 (2 件)

Tyler F
Tyler F 2022 年 2 月 15 日

0 投票

You can use the repmat function.
n=2;
N = repmat(1/n,1,n)
>> [0.5 0.5]
DGM
DGM 2022 年 2 月 15 日

0 投票

Here's one way:
n = [2 4 8 16 32];
N = cell(numel(n),1);
for k = 1:numel(n)
N{k} = repmat(1/n(k),1,n(k));
end
celldisp(N)
N{1} = 0.5000 0.5000 N{2} = 0.2500 0.2500 0.2500 0.2500 N{3} = 0.1250 0.1250 0.1250 0.1250 0.1250 0.1250 0.1250 0.1250 N{4} = 0.0625 0.0625 0.0625 0.0625 0.0625 0.0625 0.0625 0.0625 0.0625 0.0625 0.0625 0.0625 0.0625 0.0625 0.0625 0.0625 N{5} = Columns 1 through 22 0.0312 0.0312 0.0312 0.0312 0.0312 0.0312 0.0312 0.0312 0.0312 0.0312 0.0312 0.0312 0.0312 0.0312 0.0312 0.0312 0.0312 0.0312 0.0312 0.0312 0.0312 0.0312 Columns 23 through 32 0.0312 0.0312 0.0312 0.0312 0.0312 0.0312 0.0312 0.0312 0.0312 0.0312

カテゴリ

ヘルプ センター および File Exchange2-D and 3-D Plots についてさらに検索

タグ

質問済み:

2022 年 2 月 15 日

コメント済み:

2022 年 2 月 15 日

Community Treasure Hunt

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

Start Hunting!

Translated by