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!