0
votes

I have made a simulation and the result each time of the simulation is a matrix and i choose a certain row from the matrix, so if the simulation run=500, i'll have a 500 matrix and ,the row i choose each time will be (at the end of the simulation) 500 rows [one row from the first matrix...last row from the last matrix]... the problem is some times a matrix dose not contain the certain row i want , the answer is for example empty matrix: 0-by-6 i want to ignor this answer Note: the row i choose is not necessary to be exist in all matrices so if the run=600 , result in 600 matrix , the row i choose maybe =400 only and the other 200 will be zero the simulation STOP when the result is empty matrix: 0-by-any number I use Matlab

1

1 Answers

1
votes

you can use isempty to detect empty arrays, for example

a=zeros(0,5)
isempty(a)
a =
   Empty matrix: 0-by-5
ans =
     1

For when the index exceeds matrix dimensions, you can add a condition that tests the size of your matrix, specifically, how man rows using size(m,1)

So all together, in your for loop you can code something like:

for n=1:blah
    if ~isempty(M)      % continue if  matrix is non-empty
        if size(M,1)<=n % continue if index doesn't exceeds matrix dimensions
              ....
              ....