Add a known column to a matrix 'n' times without a loop?

2 views (last 30 days)
Hi Guys!
Is there a way of qriting the script below without the loop? I'm sure there's a way but I can't find it in the help section or the internet.
I want to add a known column of numbers to a matrix 'n' times.
Thank you!
Ken
clear all
clc
a=zeros(5,6); % matrix a
[m,n]=size(a);
b=zeros(m,n); % matrix b
b(1:10)=[8 8 8 8 8 10 10 10 10 10];
for i=3:n;
b(:,i)=[1 2 3 4 5];
end
  4 Comments

Sign in to comment.

Accepted Answer

Andrei Bobrov
Andrei Bobrov on 22 May 2011
b(:,k:n) = (1:5)'*ones(1,n-k+1);
more
V = 1:5;
b(:,k:n) = V(ones(1,n-k+1),:).';
more more
b(:,k:n) = repmat(1:5,n-k+1,1).';
  3 Comments

Sign in to comment.

More Answers (1)

Fangjun Jiang
Fangjun Jiang on 22 May 2011
help repmat

Community Treasure Hunt

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

Start Hunting!