How can i create matrix automatically?

11 ビュー (過去 30 日間)
Volkan Yangin
Volkan Yangin 2017 年 4 月 13 日
編集済み: Volkan Yangin 2017 年 4 月 13 日
Hi everbody
I should create a matrix by using number of element of any gap like as the example:
If gap has:
3 elements, matrix: [1 1 2]
4 elements, matrix: [1 1 2 2]
7 elements, matrix: [1 1 2 2 3 3 4] etc.
and matrix includes only 1 2 3 4 numbers, same numbers must be two side by side.
Is there any way for this operation?
Thanks...
Edit:
I have just created a loop for this operation:
for ...
elements=[1 1 2 2 3 3 4 4];
if numel(gap)<=numel(elements);
matrix=elements(1:numel(gap));
end
if numel(gap)>numel(elements);
diff=(numel(gap)-numel(elements))
matrix=[elements linspace(4,4,diff)]
end
end

採用された回答

Stephen23
Stephen23 2017 年 4 月 13 日
>> fun = @(n)ceil((1:n)/2);
>> fun(3)
ans =
1 1 2
>> fun(4)
ans =
1 1 2 2
>> fun(7)
ans =
1 1 2 2 3 3 4
  1 件のコメント
Volkan Yangin
Volkan Yangin 2017 年 4 月 13 日
編集済み: Volkan Yangin 2017 年 4 月 13 日
Thank you Stephen Cobeldick. By the time i have just created if-end loop for this operation and added under the your comment.

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

その他の回答 (1 件)

Volkan Yangin
Volkan Yangin 2017 年 4 月 13 日
編集済み: Volkan Yangin 2017 年 4 月 13 日
for ...
elements=[1 1 2 2 3 3 4 4];
if numel(gap)<=numel(elements);
matrix=elements(1:numel(gap));
end
if numel(gap)>numel(elements);
diff=(numel(gap)-numel(elements))
matrix=[elements linspace(4,4,diff)]
end
end

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by