Say I am given three n-dimesional vectors and an array T. Is there a way to compute the quantity without using a for loop?
MATLAB: Product of three 1d vectors and a 3d array
arrayMATLAB
Related Solutions
Perhaps this will help
% Concentration Distribution of contaminant (C)
T = 4000*24; % [hrs] total time
D = (1E-6)*3600; % [m^2/hr] diffusion constant
V = (1E-7)*3600; % [m/hr] velocity
L = 100; % [m] Total length
N = 20; % apatial grid sections
M = 40; % temporal grid sections
dx = L/N; % spatial spacing
dt = T/M; % time spacing
C=zeros(N+1,M+1); % allocate space for concentrations
% (C(x,t+dt) - C(x,t))/dt = -V*(C(x,t)-C(x-dx,t))/dx
% +D*(C(x+dx,t)-2*C(x,t)+C(x-dx,t))/dx^2
%
% C(x,t+dt) = C(x,t) + (D*dt/dx^2)*(C(x+dx,t)-2C(x,t)+C(x-dx,t))
% - (V*dt/dx)*(C(x,t)-C(x-dx,t))
F = D*dt/dx^2;G = V*dt/dx;C(1,:) = 100; % inlet concentration set to 100 for all time
for c = 1:M % timesteps
for r = 2:N % spatial steps
C(r,c+1) = C(r,c)+F*(C(r+1,c)-2*C(r,c)+C(r-1,c)) ... -G*(C(r,c)-C(r-1,c)); end endt = 0:dt:T;x = 0:dx:L;plot(x,C(:,M/2),x,C(:,M)),gridxlabel('x'),ylabel('C')legend(['C at time ' num2str(M*dt/(2*24)) ' days'],['C at time ' num2str(M*dt/24) ' days']) figure[t,x] = meshgrid(t,x);surf(t,x,C)xlabel('time [hrs]'),ylabel('distance [m]'),zlabel('Concentration')
C = cell(size(A));for k = 1:numel(C) C{k} = nchoosek(A,k);end
contained inside this cell array:
>> C{:}ans = 1 2 3 4 5ans = 1 2 1 3 1 4 1 5 2 3 2 4 2 5 3 4 3 5 4 5ans = 1 2 3 1 2 4 1 2 5 1 3 4 1 3 5 1 4 5 2 3 4 2 3 5 2 4 5 3 4 5ans = 1 2 3 4 1 2 3 5 1 2 4 5 1 3 4 5 2 3 4 5ans = 1 2 3 4 5
Without padding they will not fit into one numeric array, as they have different number of columns. To pad them try this:
C = cell(size(A));for k = 1:numel(C) tmp = nchoosek(A,k); C{k} = [tmp,NaN(size(tmp,1),numel(A)-k)];endN = cell2mat(C);
which creates this numeric array:
>> NN = 1 NaN NaN NaN NaN 2 NaN NaN NaN NaN 3 NaN NaN NaN NaN 4 NaN NaN NaN NaN 5 NaN NaN NaN NaN 1 2 NaN NaN NaN 1 3 NaN NaN NaN 1 4 NaN NaN NaN 1 5 NaN NaN NaN 2 3 NaN NaN NaN 2 4 NaN NaN NaN 2 5 NaN NaN NaN 3 4 NaN NaN NaN 3 5 NaN NaN NaN 4 5 NaN NaN NaN 1 2 3 NaN NaN 1 2 4 NaN NaN 1 2 5 NaN NaN 1 3 4 NaN NaN 1 3 5 NaN NaN 1 4 5 NaN NaN 2 3 4 NaN NaN 2 3 5 NaN NaN 2 4 5 NaN NaN 3 4 5 NaN NaN 1 2 3 4 NaN 1 2 3 5 NaN 1 2 4 5 NaN 1 3 4 5 NaN 2 3 4 5 NaN 1 2 3 4 5
Best Answer