Filling 2D array with a function, all at once.

6 views (last 30 days)
Hello,
I would like to fill arrays of given dimensions by a function.
% Alfas e betas [ms^-1]
alfan(1:nn,1:np) = an(vm);
betan(1:nn,1:np) = bn(vm);
alfam(1:nn,1:np) = am(vm);
betam(1:nn,1:np) = bm(vm);
alfah(1:nn,1:np) = ah(vm);
betah(1:nn,1:np) = bh(vm);
one of functions:
function [ alfan ] = an( vm )
if vm == 10.0
alfan = 0.1;
else
alfan = (0.01*(10.0-vm))/(exp((10.0-vm)/10.0)-1.0);
end
end
It was working well for only temporal evolution:
alfan(1,1:np) = an(vm);
but now it does not:
>> Trabalho2
Warning: Rank deficient, rank = 1, tol = 3.815924e-10.
> In an (line 5)
In Trabalho2 (line 81)
Subscripted assignment dimension mismatch.
Error in Trabalho2 (line 81)
alfan(1:nn,1:np) = an(vm);
Is there any simple way to solve this problem without filling array element by element? I need at least first column filled. It can be only first row if I make alfan(1:np,1:nn).
All files attached.
Thank you in advance.

Accepted Answer

Walter Roberson
Walter Roberson on 6 Dec 2015
function [ alfan ] = an( vm )
alfan = 0.1 * ones(size(vm));
alfan(vm ~= 10) = (0.01*(10.0-vm(vm ~= 10))) ./ (exp((10.0-vm(vm ~= 10))/10.0)-1.0);
end
You were passing in a matrix of vm but testing the entire matrix with a single "if" statement. "if" applied to a matrix is only true if every element of the logical condition is true. In that case you were returning a scalar 0.1 instead of something the same size as vm.
In the case where some of the elements did not equal 1.0, the "else" was executed. In the else you had a matrix / a matrix. / is the matrix right division operator, like A / B meaning A * pinv(B) with that * meaning algebraic matrix multiplication and the pinv being the pseudo-inverse. The result you were getting from that was not the same size as vm so the overall result alfan was not the same size as you were expecting on output.
The replacement code I show here takes the time to copy the size of the input, sets the default value to the exceptional condition, and it applies the formula only to the places that are not exceptional, making sure to use the ./ element-by-element division instead of using / matrix division.
  7 Comments
Star Strider
Star Strider on 6 Dec 2015
I would use the surf (or mesh) functions. The plot3 function is not written to do what you want. You would also want to specify grid on with each of your plots.
Katarzyna Wieciorek
Katarzyna Wieciorek on 7 Dec 2015
If anyone wants to learn with me:
%3D
scs = get(groot,'ScreenSize');
figure('Name','Resultados 3D','Position',[150 100 0.8*scs(3) 0.8*scs(4)])
[X,T] = meshgrid(x, t);
%Vm
subplot(2,3,1)
surfVm = surf(X,T,Vm');
set(surfVm, 'FaceColor',[0 1 1], 'FaceAlpha', 0.5, 'EdgeAlpha', 0);
rotate3d on;
xlabel('Distância','FontSize',12);
ylabel('Tempo','FontSize',12);
zlabel('Potencial da membrana [mV]','FontSize',12);
% Correntes
subplot(2,3,2)
surf1 = surf(X,T,INa');
set(surf1, 'FaceColor',[0 0 1], 'FaceAlpha', 0.5, 'EdgeAlpha', 0);
%axis([0 T -5 Is+5])
rotate3d on;
xlabel('Distância','FontSize',12);
ylabel('Tempo','FontSize',12);
zlabel('Correntes [uA/cm^2]','FontSize',12);
hold on
surf2 = surf(X,T,IK');
set(surf2, 'FaceColor',[1 0 0], 'FaceAlpha', 0.5, 'EdgeAlpha', 0);
surf3 = surf(X,T,Im');
set(surf3, 'FaceColor',[0 1 0], 'FaceAlpha', 0.5, 'EdgeAlpha', 0);
hold off
legend('INa','IK','Im');
subplot(2,3,3)
surfIain = surf(X,T,Iain',gradient(Iain));
set(surfIain, 'FaceColor',[0 1 1], 'FaceAlpha', 0.5, 'EdgeAlpha', 0);
rotate3d on;
xlabel('Distância','FontSize',12);
ylabel('Tempo','FontSize',12);
zlabel('Corrente axial intracelular [uA/cm^2]','FontSize',12);
%gNa, gK
subplot(2,3,4)
surf4 = surf(X,T,gNa');
set(surf4, 'FaceColor',[0 0 1], 'FaceAlpha', 0.5, 'EdgeAlpha', 0);
rotate3d on;
xlabel('Distância','FontSize',12);
ylabel('Tempo','FontSize',12);
zlabel('Condutancias do sodio e potassio [mS/cm^2]','FontSize',12);
hold on
surf5 = surf(X,T,gK');
set(surf5, 'FaceColor',[1 0 0], 'FaceAlpha', 0.5, 'EdgeAlpha', 0);
hold off
legend('gNa','gK');
%n, m, h
subplot(2,3,5)
surf6 = surf(X,T,n');
set(surf6, 'FaceColor',[0 0 1], 'FaceAlpha', 0.5, 'EdgeAlpha', 0);
rotate3d on;
xlabel('Distância','FontSize',12);
ylabel('Tempo','FontSize',12);
zlabel('Gating','FontSize',12);
hold on
surf7 = surf(X,T,m');
set(surf7, 'FaceColor',[1 0 0], 'FaceAlpha', 0.5, 'EdgeAlpha', 0);
surf8 = surf(X,T,h');
set(surf8, 'FaceColor',[0 1 0], 'FaceAlpha', 0.5, 'EdgeAlpha', 0);
hold off
legend('n','m','h');

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!