Fill a matrix without for-loop

  • Follow


This should be an easy one, but not for me :( Please help because I've got a headache from this one. Suppose t is a array, that is t=[1;2;3;4;5;6;......;100] e.g. How to fill a matrix without using for- loop in similar fashion with the following code, in order to accelerate the execution:

for i=1:100;k(i,:)=t(i,1):(t(i,1)+100);end

Thanks in advance
0
Reply Marko 11/29/2010 10:16:20 PM

One approach:

k2 = ones(length(t),length(t)+1);
k2(:,1) = t;
k2 = cumsum(k2,2);
0
Reply Matt 11/29/2010 10:27:05 PM


Another way:

k = bsxfun(@plus,t,0:100)

Bruno
0
Reply Bruno 11/30/2010 6:53:05 AM

Thanks Bruno, Matt

Works like a charm. From 2s to 0.2s. 10 times faster
0
Reply Marko 11/30/2010 9:13:43 AM

3 Replies
488 Views

(page loaded in 0.064 seconds)

Similiar Articles:













7/26/2012 2:48:16 AM


Reply: