From: Priyanka Choudhary on 29 Jul 2010 01:23 Hello Jan > > For that statement first the error generated was "not a proper CAT argument"and "dimensions mismatch" but now the loop is not ending, going for infinite iterations and one more thing is that in the statement > > A_initial=[A_initial;zeros(1,num_act*(i-1)) ones(1,num_act) zeros(1,num_act*(num-i))] > > The loop is going through infinite, although it is control by "for i = 1:num" ?! No, i is not going to infinite. It stops regularly at num. It is just really slow, because you let the variable A_initial grow in each loop. This *must* be slow. Please search the docs and this newsgroup for the term "preallocation". > > > First "A_initial" is shown as an error by having a zigzag underline. > > Even Matlab tries to tell you, that this is slow: please move the mouse to the underlined term and read the tooltip. > > > And when I'm running the same loop for a smaller value then the A_initial matrix for that 'elseif' statement is giving results of only the last iteration, not the whole loop. > > Not surprising, because you overwrite A_initial in the last iteration. > So at first take a look on how to solve this with y loop: > A_initial = zeros(num, num_act * num); % Allocate at once!!! > for i = 1:num > a = num_act * (i -1) + 1; > b = a + num_act - 1; > A_initial(i, a:b) = 1; > end > There is no need check for the first or last iteration at all. Even your original code runs find without it. "zeros(1,num_act*(i-1))" replies an empty matrix for i==1, so there is no need to catch this explicitely. > Thanks a lot... I got my problem solved by using the 'cat' command A_initial2=[] for i=1:!:num if i==1 A_initial1=[ones(1,num_act) zeros(1,num_act*(num-i))] elseif i>1 & i~=num Z=[zeros(1,num_act*(i-1)) ones(1,num_act) zeros(1,num_act*(num-i))] A_initial2=cat(1,A_initial,Z) elseif i==num A_initial3=[zeros(1,num_act*(i-1)) ones(1,num_act)] end end and then i finaly attached all these by using A_final=[A_initial1;A_initial2;A_initial3] Once again, Thanks a lot for looking into my problem. Priyanka |