Matlab Producing Different Values Running Entire Script VS Single Line Runs

조회 수: 4 (최근 30일)
John
John 2023년 6월 18일
편집: dpb 2023년 6월 21일
As the title states, I am getting different values from Matlab's length function depending on whether I run the entire script (Using the "Run" button) or just line by line manually. I have no clue why this is occuring but the Matlab functions are not producing correct results themselves when this occurs. I have Matlab on two seperate machines and tried testing the code on both and found the same issue persists. I tried simplifying the code with a very trvial example with the same input structures and the error does not apply to the simpler case. It is just occuring within my main code. For context, I am generating a custom mesh in cylindrical coordinates. The issue occurs on lines 104-105:
stemp = [length(nodesPhis) length(nodesPhiMSim)];
maxNodesPhi = max(length(nodesPhis),length(nodesPhiMSim)); % Finds the max dimension required for the
% phi-component 3D matrix to be populated
If we run the script using the run button, we can see in the variable window that the vector for stemp is [40,53]:
But when I highlight the text to manually run the line, it says [60,53]:
For context, I only define stemp once and in that line 104. Nowhere else is it redefined. Not only this, but even more contradictions come up later in the code from these lines. The code uses the max element of that vector to produce a NAN matrix with one of the dimensions lengths being that max number. This matrix is called "meshPhi", it's size is (x,max(stemp),z) and it is defined as:
meshPhi = NaN(length(nodesR),maxNodesPhi,max(length(nodesZ1),length(nodesZ2))); % Create "empty" phi-mesh to populate
When I run the code with the button, it actually creates the correctly sized meshPhi matrix:
But as can be seen, the meshR and meshZ matrices which are supposed to be the same size, are using 53, and not 60. Those matrices are defined like this on lines 142 and 156:
meshZ = NaN(length(nodesR),maxNodesPhi,max(length(nodesZ1),length(nodesZ2))); % Create "empty" z-mesh to populate
meshR = NaN(length(nodesR),maxNodesPhi,max(length(nodesZ1),length(nodesZ2))); % Create "empty" r-mesh to populate
Exactly the same... so the only thing I can think of is it seems that Matlab is bugged in some way here. Nonetheless, I still wanted to post this question to see if possibly it's not a bug and instead I am potentially missing something or not using an internal function correctly.
Main Code: "TEST_DynCylMesh.m"
Supporting function: "sigdigits.m"
  댓글 수: 15
dpb
dpb 2023년 6월 19일
편집: dpb 2023년 6월 21일
@Bruno Luong -- that's the edge case, yes.
The doc actually reads as " For arrays with more dimensions, the length is max(size(X)). The length of an empty array is zero." I did leave off the for zero-length arrays part, yes, since they weren't pertinent in this particular thread.
So strictly in compliance, the form would be max(size((x))*~isempty(x)
x=zeros(1,0);
length(x)
ans = 0
max(size(x))*~isempty(x)
ans = 0
dpb
dpb 2023년 6월 19일
@John, while you've probably lost interest long ere now, with the update that height and width now operate on arrays as well, I'll change my advice to recommend using them instead.

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

제품


릴리스

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by