f



advice on cell array

I'm new at using cell arrays and I'm getting a warning message I don't
understand. In the sample function below I get a message
"The variable bpdf appears to change size on every loop iteration"

I don't understand the message since I've pre-allocated. And I really
don't understand why I don't get the same message for the variable
betaDraw.

(I'm running 7.9, if it matters.)

Any advice appreciated.
-Dick Startz

function [betaDraw, bpdf] = BSS_beta(nCells)

    if nargout == 1
        betaDraw = cell(nCells,1);
        bpdf ={};
    else
        betaDraw = {};
        bpdf = cell(nCells,1);
    end
    
    for iCell = 1:nCells
        if nargout == 1
            betaDraw(iCell) = 3;
        else
            bpdf(iCell)  = 4;
        end
    end %for
end
0
Richard
8/31/2010 7:52:26 PM
comp.soft-sys.matlab 210142 articles. 11 followers. lunamoonmoon (258) is leader. Post Follow

8 Replies
434 Views

Similar Articles

[PageSpeed] 34

On 10-08-31 02:52 PM, Richard Startz wrote:
> I'm new at using cell arrays and I'm getting a warning message I don't
> understand. In the sample function below I get a message
> "The variable bpdf appears to change size on every loop iteration"
>
> I don't understand the message since I've pre-allocated. And I really
> don't understand why I don't get the same message for the variable
> betaDraw.
>
> (I'm running 7.9, if it matters.)
>
> Any advice appreciated.
> -Dick Startz
>
> function [betaDraw, bpdf] = BSS_beta(nCells)
>
>      if nargout == 1
>          betaDraw = cell(nCells,1);
>          bpdf ={};
>      else
>          betaDraw = {};
>          bpdf = cell(nCells,1);
>      end
>
>      for iCell = 1:nCells
>          if nargout == 1
>              betaDraw(iCell) = 3;
>          else
>              bpdf(iCell)  = 4;
>          end
>      end %for
> end

If you change the second nargout test to ~= from == the error stays with the 
same line, showing that Matlab is not able to analyze through all the logical 
cases to determine whether the warning can be proven to be warranted: instead, 
the warning must be somehow positional.

And indeed, if you switch the order of the branches of the first "if" and 
change the test there to ~= from ==, the error moves to betaDraw.

This suggests that Matlab is supposing that the first branch of the first "if" 
will be the one executed, and carries through that assumption to the loop but 
does not associate the "if" condition in the loop with the first "if" condition.
0
Walter
8/31/2010 9:09:11 PM
Richard Startz <richardstartz@comcast.net> wrote in message <m5nq76tgv7a3dslh32re1hnttjn8881a7l@4ax.com>...
> I'm new at using cell arrays and I'm getting a warning message I don't
> understand. In the sample function below I get a message
> "The variable bpdf appears to change size on every loop iteration"
> 
> I don't understand the message since I've pre-allocated. And I really
> don't understand why I don't get the same message for the variable
> betaDraw.
> 
> (I'm running 7.9, if it matters.)
> 
> Any advice appreciated.
> -Dick Startz
> 
> function [betaDraw, bpdf] = BSS_beta(nCells)
> 
>     if nargout == 1
>         betaDraw = cell(nCells,1);
>         bpdf ={};
>     else
>         betaDraw = {};
>         bpdf = cell(nCells,1);
>     end
>     
>     for iCell = 1:nCells
>         if nargout == 1
>             betaDraw(iCell) = 3;
>         else
>             bpdf(iCell)  = 4;
>         end
>     end %for
> end

% If you are not going to return bpdf from your function, then why define it?
% Change:

>     if nargout == 1
>         betaDraw = cell(nCells,1);
>         bpdf ={};
>     else
>         betaDraw = {};
>         bpdf = cell(nCells,1);
>     end

% to:

>     if nargout == 1
>         betaDraw = cell(nCells,1);
>         %bpdf ={};
>     else
>         betaDraw = {};
>         bpdf = cell(nCells,1);
>     end

% and see what happens.
0
someone
8/31/2010 9:26:04 PM
On Tue, 31 Aug 2010 16:09:11 -0500, Walter Roberson
<roberson@hushmail.com> wrote:

>On 10-08-31 02:52 PM, Richard Startz wrote:
>> I'm new at using cell arrays and I'm getting a warning message I don't
>> understand. In the sample function below I get a message
>> "The variable bpdf appears to change size on every loop iteration"
>>
>> I don't understand the message since I've pre-allocated. And I really
>> don't understand why I don't get the same message for the variable
>> betaDraw.
>>
>> (I'm running 7.9, if it matters.)
>>
>> Any advice appreciated.
>> -Dick Startz
>>
>> function [betaDraw, bpdf] = BSS_beta(nCells)
>>
>>      if nargout == 1
>>          betaDraw = cell(nCells,1);
>>          bpdf ={};
>>      else
>>          betaDraw = {};
>>          bpdf = cell(nCells,1);
>>      end
>>
>>      for iCell = 1:nCells
>>          if nargout == 1
>>              betaDraw(iCell) = 3;
>>          else
>>              bpdf(iCell)  = 4;
>>          end
>>      end %for
>> end
>
>If you change the second nargout test to ~= from == the error stays with the 
>same line, showing that Matlab is not able to analyze through all the logical 
>cases to determine whether the warning can be proven to be warranted: instead, 
>the warning must be somehow positional.
>
>And indeed, if you switch the order of the branches of the first "if" and 
>change the test there to ~= from ==, the error moves to betaDraw.
>
>This suggests that Matlab is supposing that the first branch of the first "if" 
>will be the one executed, and carries through that assumption to the loop but 
>does not associate the "if" condition in the loop with the first "if" condition.

This is very helpful, thanks Walter.

Just to be sure I understand, the summary is that Matlab is giving me
an unwarranted warning?
0
Richard
8/31/2010 9:27:23 PM
On 10-08-31 04:27 PM, Richard Startz wrote:

> Just to be sure I understand, the summary is that Matlab is giving me
> an unwarranted warning?

Well, especially with reference to the response from "someone" that came in as 
you were writing your response, I would say that the warning _is_ warranted -- 
warning that your code is too complex to be easily understood.

if nargout == 1
   betaDraw{1:nCells} = 3;
else
   betaDraw = {};
   bdpf{1:nCells} = 4;
end

0
Walter
8/31/2010 9:31:28 PM
On Tue, 31 Aug 2010 21:26:04 +0000 (UTC), "someone"
<someone@somewhere.net> wrote:

>Richard Startz <richardstartz@comcast.net> wrote in message <m5nq76tgv7a3dslh32re1hnttjn8881a7l@4ax.com>...
>> I'm new at using cell arrays and I'm getting a warning message I don't
>> understand. In the sample function below I get a message
>> "The variable bpdf appears to change size on every loop iteration"
>> 
>> I don't understand the message since I've pre-allocated. And I really
>> don't understand why I don't get the same message for the variable
>> betaDraw.
>> 
>> (I'm running 7.9, if it matters.)
>> 
>> Any advice appreciated.
>> -Dick Startz
>> 
>> function [betaDraw, bpdf] = BSS_beta(nCells)
>> 
>>     if nargout == 1
>>         betaDraw = cell(nCells,1);
>>         bpdf ={};
>>     else
>>         betaDraw = {};
>>         bpdf = cell(nCells,1);
>>     end
>>     
>>     for iCell = 1:nCells
>>         if nargout == 1
>>             betaDraw(iCell) = 3;
>>         else
>>             bpdf(iCell)  = 4;
>>         end
>>     end %for
>> end
>
>% If you are not going to return bpdf from your function, then why define it?
>% Change:
>
>>     if nargout == 1
>>         betaDraw = cell(nCells,1);
>>         bpdf ={};
>>     else
>>         betaDraw = {};
>>         bpdf = cell(nCells,1);
>>     end
>
>% to:
>
>>     if nargout == 1
>>         betaDraw = cell(nCells,1);
>>         %bpdf ={};
>>     else
>>         betaDraw = {};
>>         bpdf = cell(nCells,1);
>>     end
>
>% and see what happens.

The function I pasted in was simplified so as to not make readers plow
through more than was necessary. In my real application the cells
aren't set to constants.

So I think that what I've done is simple and efficient, but I haven't
done this before and would be happy to hear advice on how to do it
better.

function [betaDraw, bpdf] = BSS_beta(nCells, vBar, V0_inv_beta0,...
    hDraw_epsilon, xPy, betaStar)

    if nargout == 1
        betaDraw = cell(nCells,1);
        bpdf ={};
    else
        betaDraw = {};
        bpdf = cell(nCells,1);
    end
    
    for iCell = 1:nCells
        betaBar =  ...
            vBar{iCell}*(V0_inv_beta0{iCell} ...
            + hDraw_epsilon*xPy{iCell});
        if nargout == 1
            betaDraw(iCell) = mvnrnd(betaBar,vBar{iCell});
        else
            bpdf(iCell)  = mvnpdf(betaStar, betaBar,vBar{iCell});
        end
    end %for
end
0
Richard
8/31/2010 9:41:52 PM
On 10-08-31 04:41 PM, Richard Startz wrote:

> The function I pasted in was simplified so as to not make readers plow
> through more than was necessary. In my real application the cells
> aren't set to constants.
>
> So I think that what I've done is simple and efficient, but I haven't
> done this before and would be happy to hear advice on how to do it
> better.

I would still recommend splitting the cases. You do not have so much code that 
doing so would make the function messy (it would probably end up shorter).

I would also suggest that you consider having a single output and passing in a 
flag indicating which of the two variations you wanted.


> function [betaDraw, bpdf] = BSS_beta(nCells, vBar, V0_inv_beta0,...
>      hDraw_epsilon, xPy, betaStar)
>
>      if nargout == 1
>          betaDraw = cell(nCells,1);
>          bpdf ={};
>      else
>          betaDraw = {};
>          bpdf = cell(nCells,1);
>      end
>
>      for iCell = 1:nCells
>          betaBar =  ...
>              vBar{iCell}*(V0_inv_beta0{iCell} ...
>              + hDraw_epsilon*xPy{iCell});
>          if nargout == 1
>              betaDraw(iCell) = mvnrnd(betaBar,vBar{iCell});
>          else
>              bpdf(iCell)  = mvnpdf(betaStar, betaBar,vBar{iCell});
>          end
>      end %for
> end

0
Walter
8/31/2010 9:57:48 PM
On Tue, 31 Aug 2010 16:57:48 -0500, Walter Roberson
<roberson@hushmail.com> wrote:

>On 10-08-31 04:41 PM, Richard Startz wrote:
>
>> The function I pasted in was simplified so as to not make readers plow
>> through more than was necessary. In my real application the cells
>> aren't set to constants.
>>
>> So I think that what I've done is simple and efficient, but I haven't
>> done this before and would be happy to hear advice on how to do it
>> better.
>
>I would still recommend splitting the cases. You do not have so much code that 
>doing so would make the function messy (it would probably end up shorter).
>
>I would also suggest that you consider having a single output and passing in a 
>flag indicating which of the two variations you wanted.
>
>

Thanks for the good advice.
-Dick
0
Richard
8/31/2010 10:10:54 PM
On 10-08-31 05:10 PM, Richard Startz wrote:
> On Tue, 31 Aug 2010 16:57:48 -0500, Walter Roberson
> <roberson@hushmail.com>  wrote:

>> I would also suggest that you consider having a single output and passing in a
>> flag indicating which of the two variations you wanted.

You could also consider using two different routines; then the code for each 
would be relatively simple.
0
Walter
8/31/2010 10:21:19 PM
Reply: