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 206961 articles. 560 followers. lunamoonmoon (258) is leader. Post Follow

8 Replies
325 Views

Similar Articles

[PageSpeed] 5
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:
Similar Artilces:

Textfile to array or hash
Hello, I want to extract data from a text file I recieve from the internet to an array or hash. So far I did not found a solution via google and co - maybe of my bad english and wrong search term. The file is put into a varaible : --------------------- $simartistsAS = `wget -q -O - "http://ws.audioscrobbler.com/1.0/artist/ $m/similar.txt"`; --------------------- and looks like this: --------------------- 100,,Medeski, Martin and Wood 69.54,e0953daa-860f-4dc8-9f1a-b12587cdaf17,Tortoise 49.98,9de8f66e-3cd1-4f11-8328-38200f0612b0,Doves 45.19,b7834ebd-64ae-46c3-a930-2d3a52ee743a,The Wa...

anti-spam advice
Goal: avoid seeing spam (see note at bottom) without wrongly rejecting non-spam messages. Setting Symbiont-Checks-Deliverability to FALSE stops probably 80% of spam. However, the remaining 20% is too much. I experimented with routing my incoming email through a system running SpamAssassin, tagging messages with score > x and rejecting those with score > y. However, I couldn't set y low enough to reject all spam without rejecting legitimate messages. That meant that the messages addressed to non-existent users were initially accepted by the system running SpamAssassin, so ...

how to jump from one cell to the other using the keyboard?
I'd like to be able to jump to the first position of the next cell using say PAGE-DOWN key (or any other key is ok). (also jump to the above cell using the PAGE-UP key) Is there a way to do that? I looked at http://reference.wolfram.com/mathematica/tutorial/KeyboardShortcutListing.html and do not see one. i.e. the cursor might be in the middle of a cell, and I'd like it to go to jump to start of the next cell (or the above cell) using one key hit. (I am getting tired of using the arrow key to scroll down and up the notebook) thanks, --Nasser ...

cell array to csv
Hi, I was wondering if there a way to write a cell array to a csv file. There doesn't seem to be a function in matlab toolkit. Probably there is a way, but I'm not aware of how to do it. Thanks in advance. On 02/10/10 10:39 AM, jay jay wrote: > I was wondering if there a way to write a cell array to a csv file. > There doesn't seem to be a function in matlab toolkit. Probably there is > a way, but I'm not aware of how to do it. xlswrite() might be able to handle it for you (though it doesn't really document it), but only if you on on MS Windows an...

Need advice on the design of my application
I am writing a hull-wave simulator for the design of boat hulls as they are affected by waves. This application is composed of 2 main parts, the part that renders the waves and its impact on the hull, and a GUI that controls the hull shape, waves, and other factors. The two parts of the application will need to exchange data, for example, the GUI will set the hull shape and this will be displayed by the renderer. The renderer will show the waves and it will tell the GUI statistics about the waves as they are being animated. This application will run on Windows. The Renderer par...

question about longtables and array
I tried to use longtables and array with the last column having a default \raggedright declaration, viz. \begin{longtable}{l>{\raggedright}p{3.0in}} This didn't work. No matter what the second line of the table was, including \hline, I got a complaint. If I put an empty column at the end, viz. \begin{longtable}{l>{\raggedright}p{3.0in}l} it works, so long as I explicitly have "contents" for the empty column, i.e., the lines have to end with "&\\" Is this a bug or a feature of the conspiracy of longtable and array packages? BTW, I used the packages i...

How initialize an array to 0's?
I have the following correct sentence $flagi[1..$jornadas][1..$partidos] = 0; that doesn't do anything useful. What is the proper and modern mode to do the thing? Thanks Do you really mean to have a 2d array? Anyway, use slices. Plus maybe some perl-ish tricks to quickly generate the list of 0s. In the 1d case: #be careful, you probably want to start indexing at 0, not 1 #leaving it your way for now @flagi[1..$jornadas]=split(/\s/,"0 "x$partidos); If you really meant 2d then: @flagi; $flagi[$jornadas]=undef;#the first dimension is grown to have ...

need help on speed up (putting cell array items into vector)
Hi, I have a cell array of structures ("IP") which contains element x. I want to put the x of "some" of these IP into vector v. But the following code is slow. v = []; inds = [2 3 5]; for i=1:length(inds) ind = inds(i); v = [v, IP{ind}.x]; end Any suggestions on speeding this up? Thanks!! bahoo In article <1186700635.481408.265880@q3g2000prf.googlegroups.com>, bahoo <b83503104@yahoo.com> wrote: >I have a cell array of structures ("IP") which contains element x. >I want to put the x of "some" of these IP into vector v. &...

Re: still questions on arrays and missing values #5 637837
I really appreciate everyone's help!!! Thank you so much!!! Please note that this was a make-up dataset and is not related to real medical data. The hypothesis we are examining does not allow use of data past the time point of interest. Thank you again for all the help!!! Kind regards, Tina. ...

I need some advice
Hello. I'm looking for a video card for my dad. He currently has a Radeon 9700 Pro. He wants to get an NVidia card so he can use the stereo 3D in games. But I want to find him a card that won't break the bank, he doesn't want to spend too much money. So what would be a good choice? The card just has to be beter than a Radeon 9700, it doesn't have to scream, but it should also be a decent upgrade in itself (ie, so he doesn't need a new card in a year). He doesn't have much interest in anti-aliasing or anything like that. I'm having a heck of a time f...

Advice on adding memory to MSI 865PE-FIS2R
I am planning on upgrading my home brew computer from Win98/512MB to WinXP Media Center/1GB. I currently have two sticks of KHX3200K2/256MB and I have the timings set to 2-2-2-6 with MAT/PAT enabled. According to Memtest86 I am getting a write speed of 2570MB. I see that Kingston no longer makes this memory. Should I get 2 more sticks of 256MB, such as: OCZ Enhanced Latency 512MB (2 x 256MB) (2-3-2-5): http://www.newegg.com/Product/Product.asp?Item=N82E16820146895 Kingston HyperX 512MB (2 x 256MB) (2-3-2-6): http://www.newegg.com/Product/Product.asp?Item=N82E16820144113 ...

??? Undefined function or method 'eq' for input arguments of type 'cell'
When I write the m file as below : for i=size(name,1) for j=size(bravo,1) if name(2:i,1)==bravo(1:j,1); ans==databv(j-4,:); end end end it shows ??? Undefined function or method 'eq' for input arguments of type 'cell' actually, i think the file in the name and bravo workspace is string or the text, what should i correct? i've tried the function of for i=size(name,1) for j=size(bravo,1) if strcmp(name(2:i,1),strcmp(bravo(1:j,1)))==1 ans==databv(j-4,:); end end end and the result is ??? Error us...

[9fans] advice and advice
i'm doing a cross the Himalayas by bunnie. so my advice is if anyone wants to tag along it's july 17th. i'm fitting out the support jeep (all else are on mad motobikes) and i'm having problems with accelerometers. the ones i've tried either jitter inanely or go erratic. any experience / advice? and i need a 2nd tech so i can sleep. cinap? brucee ...

Re: Cell Phone Incident Results in DC Metro Arrest #8
On Tue, 05 Oct 2004 02:01:08 GMT, Tony P. <kd1s@nospamplease.verizon.reallynospam.net> wrote: > Instead of placing the blame on the woman, place it on the crappy cell > phones we have. The side tone is nearly absent on almost every cell > phone I've used over the years. There's a lack of sidetone on mobile phones because if there was an appreciable amount you'd get feedback. Plenty of people know how to use a mobile phone without shouting. The blame should be placed squarely on the shoulders of the woman who didn't have the social skills needed...

Any advice on digital photo albums
I want to start making albums on dvd for my customers, and wonder if any of you do this, and what programs are best for it. Any ideas are greatly appreciated. Thanks, Bill I made some slideshows and primitive movies with Roxio EZ-CD creator. It's simple to use and creates .wma files. There are templates too with themed shows, you just add the pictures. I have also used the windows movie maker that came with my comp. I think too you can just put some files on a DVD and most comps nowadays will just autoplay them. "Bill and Lisa" <mc5WWW@cox-internet.com> wrote ...

Array Question? #6
Hello Everyone, =A0 I'm trying to code some adverse events. For each patient, I need to determi= ne whether=A0the event occured at or before Cycle 6 of chemotherapy as well= as whether any instances of the event up to that point were of grade3 seve= rity or higher. =A0 My list of adverse event variables is as follows: NeutropeniaYN Thrombocyto= peniaYN MucositisYN DiarrheaYN FatigueYN. =A0 I've developed the following code for the Neutropenia variable: =A0 data ae_drip (keep=3Dsubject_number race Neutropenia6 Neutropenia6gr3); set adverse_events; by subject_number; where cycle_label i...

Need to add item references to an array
My assignment is to store a bunch of furniture items in a certain storage area. I have a method that checks to make sure the item can fit. If it can fit, then i have another method that adds the item to the area. Both of these are boolean methods. Then I am supposed to have another method: contents() of type Furniture[]. The problem is I dont know how to actually add the items into contents(). heres the code: public boolean canFit(Furniture item){ return totalVolume() + item.volume() <= capacity(); } /** * Attempts to add item to this storage area....

How to return an array as an Object?
Hello, I am writing a method that returns an array as an Object. Initially there is a single dimension array Object X[]. However, I can insert other arrays as Objects in X, thus I can end up with something like following: ------------------------ | A | B | C | Null | D | ------------------------ | R | ----- | S | ----- Now I have a method called Object get(int i) which is supposed to return the object at position i of the array X. So e.g. if I call get(3) it should return Null. If I call get(2) it should return the Object[] array at in...

How to do advice
I want to add a load of error messages into a screen and they could scroll. I want the titles in bold and the rest of the text normal Will a MLE be good enough? Or do you think I may be better off with a bBrowser? Thanks Martin ERROR 1 lots of details over a few lines ERROR 2 more details like this ERROR 3 ect ect Martin, I use a Rich Edit. email me privately if you want the code. I cannot put = attachments with this news server. Regards, Willie williem@wmconsulting.com "Martin (BT)" <martin-spam@btinternet.com> wrote in message=...

Returning an array of strings to LV from activex.
Hope you gurri's can help here, can find much on net about this, just stuff on some sort of COM wrapper thingi from NI??, aint got that here.... I have an ATL project on the go which returns an array of strings to labview, which is working fine, with no problems, exept I cant 'probe' the returned VARIANT data for some reason, so me thinks its the wrong variant type!, but it works :) Is it a "variant containing a safearray of type BSTR" that labview is supposed to recieve ? If I setup a simple test vi with stringArray->toVariant->indicator, I can see the strings O...

Newbie question: removing array reference information
I'm working on a program which reads hyperlinks from a specific web page. Using the WWW::Mechanize module, I read the page in question using regex to scan for a particular URL string. Every instance of that string I find is stored in an array. However, when I read the array, instead of the matched text, I find a list of array references for example: WWW::Mechanize::Link=ARRAY(0x90c160). Regardless of what changes I make to how I read the information on the page or even what information I look for I can't escape these array references. Does anyone have a clue on how I can remove them o...

Versatile way to access _next_ element in array
I am trying to access a(end+1) where if the array is empty it will default to a(1) I get an error when I do a(end+1) on an empty array a. (These are structure arrays). Is there a good syntax to do this? In other words I want to type: a(blah) = b; where blah will either: 1. create array a if none exists and set first element to b or 2. append b to a Thank you Misha help mod In article <h4o1ai$og6$1@fred.mathworks.com>, "Misha Koshelev" <mk144210@bcm.edu> wrote: > I am trying to access > a(end+1) > where if the array is empty it will default to > a(1) ...

Creating an array of structures
Hi all, Given I have some pre-existing arrays, for example: a = 1:10; b = a*10; c = a*100; How can I simply build an array of structures, x, such that x(3).b is 30 (for example)? Thanks! Justin Cokelid <cokelid@googlemail.com> wrote in message <7e83cc97-03e0-4bed-9686-b5e1203a78c7@r40g2000yqj.googlegroups.com>... > Hi all, > > Given I have some pre-existing arrays, for example: > a = 1:10; > b = a*10; > c = a*100; > > How can I simply build an array of structures, x, such that x(3).b is > 30 (for example)? > > Thanks! > > Justin one ...

Advice please
I'm looking to implement a GUI, and was wondering how I should go about interfacing my c code with perltk. Basically, I now have a piece of perl script that doesn't do anything fancy except it passes a bunch of parsed args to a couple of c programs that are sync'ed with shared semaphores and shared memory segments whose id's are passed around by the perl script to the c code. One of the program (written in pure c) basically provides a "waveform display" right now, in ASCII format. 15642 I | 15644 I | CAS tid[0] scheduled. 15646 I...

numpy
import numpy data = numpy.array(...) numpy.save("test.np",data) This is very good, but I want to save the data into a file object with a write() method. E.g. not a real file. (My purpose right now is to save many arrays into one binary file, while recording starting positions of the arrays.) How can I do that? Thanks Laszlo Laszlo Nagy wrote: > import numpy > data = numpy.array(...) > numpy.save("test.np",data) > > This is very good, but I want to save the data into a file object with a > write() method. E.g. not a real ...