如何在不使用诸如length(),size()等函数的情况下确定Matlab中数组的元素数量? 谢谢你!
答案 0 :(得分:1)
逻辑是迭代单元格直到遇到空单元格。我已经为迭代采用了两个变量,即 i 和 j 。 i 用于行, j 用于列。
最初将i和j的值设置为<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="SanitySuite">
<listeners>
<listener class-name="XYZScripts.Listeners" />
</listeners>
<parameter name="BROWSER" value="chrome" />
<parameter name="URL" value="http://blabla.com" />
<test name="XYZTests" parallel="classes" thread-count="1">
<classes>
<class name="XYZ.Class1" />
<class name="XYZ.Class2" />
<class name="XYZ.Class3" />
</classes>
</test>
</suite>
和i=1
。现在在j=1
循环迭代中,通过递增j 和将i = 1保持为常量。
**首先尝试将该单元格复制到变量 x 中。如果该单元格是空单元格,则会弹出错误。目标是使用 try / catch 语句来利用此错误。
Scince语句while
写在x = vec(i,j);
块中,因此在遇到空单元而不是try
时,try
块将被执行catch
使标志为0,这将导致while循环结束。
因此,我们在flag = 0
中存储了列数,j
循环在遇到空单元时终止。
while
flag = 1;
i = 1;
j = 1;
while(flag==1) % loop will continue till flag is 1 (or say flag is HIGH)
try
x = vec(i,j); % Try to copy this cell of array vec in x
j = j+1; % if successful to copy then increment value of j else catch block is executed
catch
flag = 0; % Set flag = 0 (or low) to end the while loop
end
end
j = j - 1; % The value of j comes out to be one greater than number of columns hence decrementing by 1
%
% WE HAVE THE NUMBER OF COLUMNS !!
%
% WE WILL PERFORM SAME THING TO GET THE NUMBER OF ROWS BY INCREMENTING i AND
% KEEPING j AS CONSTANT
flag=1;
while(flag==1)
try
x = vec(i,j);
i = i+1;
catch
flag = 0;
end
end
i = i - 1;
% i is the number of rows in the matrix or array
% j is the number of columns in the matrix
dim = [i,j] % vector named dim(or say matrix named dim) contains the required output
包含 vec