make column in a matrix: 1,1,1,2,2,2,3,3,3 etc

5 ビュー (過去 30 日間)
Andrea
Andrea 2012 年 11 月 20 日
コメント済み: Gal 2015 年 10 月 15 日
Hi,
I'm trying to create a matrix A(21,4). In the second column I want to insert the numbers 1,1,1,2,2,2,3,3,3 etc until the end of the column. Tried this:
A = zeros(21, 4);
A(:,2) = rem((0:size(imageInformation,1)-1)',7)+1;
Which creates the column 1,2,3,4,6,7,1,2,3 etc. How can I modify this/is there any way to write code which would input the previously mentioned numbers into the matrix?
  1 件のコメント
Gal
Gal 2015 年 10 月 15 日
n = 3; repeats = 3;
x = repmat(0:1:n,repeats,1);
x = x(:)';

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

採用された回答

Jos (10584)
Jos (10584) 2012 年 11 月 20 日
A(:,2) = ceil((1:size(A,1))/3)

その他の回答 (5 件)

Matt Fig
Matt Fig 2012 年 11 月 20 日
編集済み: Matt Fig 2012 年 11 月 20 日
A(:,2) = reshape(cumsum(ones(3,7),2),21,1);
or
A(:,2) = floor(linspace(1,7.7,21));

Image Analyst
Image Analyst 2012 年 11 月 20 日
編集済み: Image Analyst 2012 年 11 月 20 日
Try this:
A(:, 2) = [1,1,1,2,2,2,3,3,3,4,4,4,5,5,5,6,6,6,7,7,7]';
If you need it more general, then say so.
If you have the Image Processing Toolbox, you can do it more generally this way:
A = zeros(21,4);
[rows columns] = size(A);
secondColumn = imresize((1:rows/3)', [rows, 1], 'nearest')
A(:, 2) = secondColumn

Wayne King
Wayne King 2012 年 11 月 20 日
One of many ways (requires Signal Processing Toolbox for upsample.m)
x = (1:7)';
x = repmat(x,1,4);
A = upsample(x,3);
A = filter(ones(3,1),1,A);

Jan
Jan 2012 年 11 月 20 日
A(:, 2) = reshape(repmat(1:7, 3, 1), [], 1);

Andrea
Andrea 2012 年 11 月 20 日
Thank you everyone! Lots of great solutions. I ended up using the one Jos suggested, but I'm very grateful to you all! :)

カテゴリ

Help Center および File ExchangeGet Started with MATLAB についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by