Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Now you have clarified your question (again...) here is a solution (still using a for loop...). It includes "rule 7" - excluding runs of fewer than three elements; it also includes the second part of that rule - runs of fewer than three zeros don't count as zero. The new code looks like this:</p> <pre><code>A = [ 0 0 0 2 4 1 0 0 0 1 3 2; 0 0 0 5 1 1 1 1 0 0 0 1; 0 3 4 1 0 3 1 2 0 0 1 3; 0 0 0 0 1 3 4 5 0 0 0 0]; retVal = cell(1, size(A, 1)); for ri = 1:size(A,1) temp = [1 0 0 0 A(ri,:) 0 0 0 1]; % pad ends with 3 zeros + 1 % so that is always a "good run" isz = (temp == 0); % find zeros - pad "short runs of 0" with ones diffIsZ = diff(isz); f = find(diffIsZ == 1); l = find(diffIsZ == -1); shortRun = find((l-f)&lt;3); % these are the zeros that need eliminating for ii = 1:numel(shortRun) temp(f(shortRun(ii))+1:l(shortRun(ii))) = 1; end % now take the modified row: nz = (temp(4:end-3)~=0); dnz = diff(nz); % find first and last nonzero elements f = find(dnz==1); l = find(dnz==-1); middleValue = floor((f + l)/2); rule7 = find((l - f) &gt; 2); retVal{ri} = middleValue(rule7); end </code></pre> <p>You have to use a cell array for the return value since you don't know how many elements will be returned per row (per your updated requirement). </p> <p>The code above returns the following cell array:</p> <pre><code>{[5 11], [6], [7], [7]} </code></pre> <p>I appear still not to understand your "rule 7", because you say that "no columns in row 3 satisfy this condition". But it seems to me that once we eliminate the short runs of zeros, it does. Unless I misinterpret how you want to treat a run of non-zero numbers that goes right to the edge (I assume that's OK - which is why you return <code>11</code> as a valid column in row 1; so why wouldn't you return <code>7</code> for row 3??)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload