Filling 2D array with a function, all at once.
1 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
采纳的回答
Walter Roberson
2015-12-6
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 个评论
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Surface and Mesh Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!