I am trying to vectorize a loop in which each column vector of a 2D matrix (n-by-n) is found by multiplying each single element in a diagonal matrix with a column vector in another n-by-n 2D matrix. It seems like it would be simple to vectorize, but I must be missing something. I tried converting the diagonal matrix to a column vector first, but it still needs to do the operation one element at a time.
Thank you in advance.
%% example matrix B = [ 3 2 .9 2 2 4 1 2 3 4 -1 0 .5 .5 .1 1]; %% find eigenvectors and eigenvalues [ve, va] = eig(B,'nobalance'); %% get size of B ... 4 [~,q]=size(B); %% time constants t0=0; t = 2000e-6; %% pre-allocate memory for Matrix Mn (Mnew) and Mp (Mprime) Mn=zeros(q,1); Mp=Mn; %% Original code, runs correctly, not vectorized - steps through each column for m = 1:q, Mn(:,m) = exp( va(m,m) * t0) * ve(:,m); Mp(:,m) = exp( va(m,m) * t ) * ve(:,m); end % quick explanation ^ for each column of Mn and Mp, a single value % exp(va(i,i)*t) is multiplied by a column of ve(:,i) % vectorizable? % Mn(column) = A * ve(column), but A is an individual element in a different vector already %% New code... vectorized? % random starting idea: va1 = va * [1; 1; 1; 1]; % changes diagonal matrix into column vector of values, BUT still has to calculate one at a time...