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

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.

 採用された回答

Walter Roberson
Walter Roberson 2015 年 12 月 6 日

0 投票

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 件のコメント

Katarzyna Wieciorek
Katarzyna Wieciorek 2015 年 12 月 6 日
編集済み: Katarzyna Wieciorek 2015 年 12 月 6 日
I have new error in the same code. If I should ask new question just tell me.
Error using plot3
Vectors must be the same length.
Error in Trabalho2 (line 164)
plot3(x,t,Vm)
x - 1 x 601
t - 1 x 10001
Vm - 601 x 10001
What should I do?
Error using mesh (line 70)
Data dimensions must agree.
Error in Trabalho2 (line 164)
mesh(x,t,Vm)
Of course the same for mesh.
Star Strider
Star Strider 2015 年 12 月 6 日
One possibility:
x = linspace(0, 1, 601); % Create Data
t = linspace(1, 2, 10001); % Create Data
Vm = randi(99, 601, 10001); % Create Data
[X,T] = meshgrid(x, t); % Create Mesh Matrices
figure(1)
mesh(X,T,Vm') % Transpose ‘Vm’ And Plot
grid on
figure(2)
plot3(X, T, Vm')
grid on
Katarzyna Wieciorek
Katarzyna Wieciorek 2015 年 12 月 6 日
Thanks a lot!
No more errors, now just aesthetics. I want to obtain look similar to effect of contour3 or surf, but contour3 doesn't like legends that I need and surf doesn't accept multiple variables:
(Warning: Ignoring extra legend entries.)
Error using surfchk (line 12)
Too many input arguments.
Error in surf (line 69)
error(surfchk(dataargs{:}));
Error in Trabalho2 (line 175)
surf(X,T,INa',X,T,IK',X,T,Im')
What can I do? Can I make plot3 look like contour3 or surf? Because now plot3 offers me only piece of brown surface :(
Star Strider
Star Strider 2015 年 12 月 6 日
I can’t figure out what you’re doing.
If you want to overplot several surf plots, use the hold function. If you want to modify it, use the get and set functions. (That might also work for plot3, but it’s designed for 3D line plots, so the results you get from it may not be optimal.)
Katarzyna Wieciorek
Katarzyna Wieciorek 2015 年 12 月 6 日
^This is result of plot3, I find this illegible and I don't like it. I would like to see something similar to this:
Sometimes I have to plot 2 or 3 different variables and I need correct legend for them. Someone who sees the graphics should be able to recognize variables (having legend) and their evolution in time and space.
Star Strider
Star Strider 2015 年 12 月 6 日
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 2015 年 12 月 7 日
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');

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Community Treasure Hunt

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

Start Hunting!

Translated by