advice on cell array

  • Permalink
  • submit to reddit
  • Email
  • Follow


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
Reply Richard 8/31/2010 7:52:26 PM

See related articles to this posting


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
Reply 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
Reply 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
Reply 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
Reply 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
Reply 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
Reply 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
Reply 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
Reply Walter 8/31/2010 10:21:19 PM
comp.soft-sys.matlab 203603 articles. 536 followers. Post

8 Replies
298 Views

Similar Articles

[PageSpeed] 17


  • Permalink
  • submit to reddit
  • Email
  • Follow


Reply:

Similar Artilces:

for loop to open text files and place in cell array? any advice
I have 100 text files in a directory and each file has numeric contents, i.e 4.5 5.4 6.7 8.0...etc. I'd like to loop through the directory and place the contents of each text file into a cell array in matlab. I'd like to store them so cell{1,1} would have 1.txt cell {2,1} would have 2.txt and so on. I've played with the contents function however, I have to store the matlab m file in a separate directory than the actual text files. So I'm forced to use the full file extension when I pull the text files, 'c://location/a/b/.../1.txt' I'm able to accomplish this whe...

convert to cell array into a character array or cell array of strings
Hi I have this horizontal array of names: Name Size Bytes Class Attributes itemNames 1x70 7428 cell I need to convert this into a a character array or cell array of strings. I tried: S = char( itemNames) ??? Error using ==> char Cell elements must be character arrays. I tried also: S = cellstr( itemNames) ??? Error using ==> cellstr at 34 Input must be a string. I appreciate your help, thanks, Manal "Manal " <news@mathworks.com> wrote in message <guqdbh$qci$1@fred.mathworks.com>... &...

Cell arrays containing cell arrays
Hi Everyone, I have data in cell arrays containing cell arrays. I would like to extract data from these cell arrays and store in a normal array. I am able to do it for a particular case using nested for loops. But I would like to generalize this problem such that I can extract any kind of cell array containing cell arrays just like cell2mat function does. Please let me know if there is any solution to this problem. Thanks, Anil Palaparthi. Already discussed here: http://www.mathworks.com/matlabcentral/newsreader/view_thread/253738 Bruno Anil Kumar Palaparthi wrote: > I ha...

Access to cell array in cell array
Hallo. I have a problem with a cell array. It's a 12x2 cell array. And in the second column in each row of this cell array is again a 13x1 cell array. In the 8th cell there is a column vector. I tried to gain access to this column vector in the 8th cell for finding the minimum at once of all 12 cells in the second column. But I failed. It looks like that: samples_merged{i,2}{8,:} where i runs from 1 to 12. I am able to solve the problem with a loop, sure, but if anyone comes up with an idea without a loop, I would be happy. Kind regards, Gudrun In article <10143048.1204191328840.J...

Changing matrices in cell arrays except for one cell array in a cell
Hey guys I need your help once again! I hope someone can show me how to do this. So I have a cell MTT with "d" cell arrays in it, each cell array contains a matrix of nxn and I need to change a whole row of numbers to Inf in every matrix except for one of them which is going to be given by variable "b". MP is a matrix with a dx2 dimension. Here is my code but this will do it for all matrices, I want to find a way to not do it for "b", for d = 1:numMachines; MTT{d}(MP(b,2),:) = Inf; end Now I have tried doing the following but I guess I can not do...

Expanding Cell Array within a Cell Array
I have a 10000x1 cell array, and within each of those cells a 1x5 cell array. Every cell contains numbers stored as characters. How do i go about converting it to a 10000x5 matrix of doubles? Thankyou in advanced "Jordan" wrote in message <jp1gn9$krj$1@newscl01ah.mathworks.com>... > I have a 10000x1 cell array, and within each of those cells a 1x5 cell array. > > Every cell contains numbers stored as characters. > > How do i go about converting it to a 10000x5 matrix of doubles? > > Thankyou in advanced Jordan: This probably isn't the be...

Part of cell array into another cell array
Hello I would like to know how can I put a part of a cell array into another cell array without use for or while loops. For example: "points" is a cell array of dimensions 1 x 16. The elements of "points" consists on matrices of several sizes. Now I want to introduce "points{1:5}" into another cell array of dimensions 1 x 5 without using loops. How can I do this? Thanks in advance. help deal e.g. [points2{2:6}]=deal(points{1:5}); Jos� L. G�mez wrote: > Hello > > I would like to know how can I put a part of a cell ...

Processing Cell Array of Cell Array of Strings
Hi, So I have a cell array of a cell array of strings that I am working with. As an example consider the following: test=cell(5,1); test{1,1}{1}=num2str(1); test{1,1}{2}=num2str(0); test{2,1}{1}=num2str(0); test{2,1}{2}=num2str(0); test{3,1}{1}=num2str(1); test{3,1}{2}=num2str(1); test{4,1}{1}=num2str(0); test{4,1}{2}=num2str(0); test{4,1}{3}=num2str(0); test{5,1}{1}=num2str(0); test{5,1}{2}=num2str(1); test{5,1}{3}=num2str(0); I want to process this cell array of cell array of strings so that the inside cell array is removed to make it easier to analyze and look at the...

Breaking Up Large Cell Array into Smaller Cell Arrays
Hello, I have a unique problem. Right now I have a large cell array and I want to break that cell array into smaller cell arrays. Here is a portion of the large cell array: C = [1x35 char] [1x39 char] 'ALAMEDA' 'CA' [1x30 char] [1x20 char] 'ALAMEDA' 'CA' [1x30 char] [1x20 char] 'LUDINGTON' 'MI' [1x30 char] [1x20 char] 'LUDINGTON' 'MI' Now, for this test case I want to break this into two smaller cell arrays according to...

Converting cell array of strings to cell array of numbers
I have x = Columns 1 through 11 '1' '10' '11' '12' '13' '14' '15' '16' '2' '3' '4' Columns 12 through 22 '5' '6' '7' '8' '9' '1' '10' '11' '12' '13' '14' Columns 23 through 33 '15' '16' '2' '3' '4' '5' '6' '7' '8' '9' '1' Co...

Assignment of a cell array to a portion of another cell array
Hello, I have a question. I think my problem has to do with a fundamental lack of knowledge about how indexing works in Matlab. Here is my code: Assume D3 is a data structure containing 3 fields, each field is an nx1 array. D3.F1 = [1 2 3 4] D3.F2 = [5 6 7 8] FL = fieldnames(D3); data{:,3} = num2cell( D3.(FL{2}) ) %so I am trying to fill the third column of cell array "data" with the contents of the array of the second field of D3. What I get is data{1,1} containing a cell which contains the entire cell array of D3.F2. Can someone tell me how to make this assi...

function that takes a cell array and outputs cell array
Hello I'm sorry if this is a really obvious problem but I am having a hard time finding the specific answer to this on the internet. I am trying to write a function that has a cell array as an input argument and outputs a cell array. I have written the following lines, which at this point should just output the same array as was input: function outTable = coltable(inTable) for i = 1:size(outTable,1) for j = 1:size(outTable,2) outTable{i,j} = inTable{i,j}; end end However, when I try to call the function using the following lines: B = {1 2 3 ; 4 5 6} outGU...

How to sort cell array of cell arrays by inner value
I have a 1 x 500 cell array where each cell is a 1 x 4 cell array. The inner 1 x 4 cell array has a double numeric value in the 4th cell. I would like to sort the 500 cells by this numeric value. What is the best way to do this? Thanks In article <1191983851.468137.61110@r29g2000hsg.googlegroups.com>, G.A.M. <x0Zero@gmail.com> wrote: >I have a 1 x 500 cell array where each cell is a 1 x 4 cell array. The >inner 1 x 4 cell array has a double numeric value in the 4th cell. I >would like to sort the 500 cells by this numeric value. What is the >best way to do this? Thank...

Create 4x4 Cell Array and not 1x1 Cell Array
Hello, I have the following: A = [1 2; 4 5] And B = 'hello' I want to create two cell arrays where each element is a 1 by 1 matrix. Something like I would have doing: A_Cell = {1 2; 4 5} and B_Cell = {'hello'} If I do A_Cell = {A} and B_Cell = {B} I don't get what I want. Can somebody tell me how to do this? Thanks, Miguel A_cell = num2cell(A(:)); B_cell = num2cell(B(:)); Are they what you want? ...

Assigning Cell Array to Portion of Another Cell Array
I have a bunch of 1D cell arrays that I wish to combine into a 2d cell array matrix. I am having trouble coming up with a statement that will do this. My current plan of action is to create my 2D cell array and then assign each 1D array to a particular column of the 2D array. So I have a 1D cell array: a = 'ay' 'be' 'ce' 'de' I make a 2D cell array: c = cell(4,10); I want to stick the contents of a{1} into c{1,5}. Then I want to stick the contents of a{2} into c{2,5}. and so on for all 4 elements of a. when I try the stat...

How do I extract a character array from a cell array (so that each cell = a row)??
I have a text file that I read in to Matlab using textscan, and have the data extracted into cell arrays. The cell array I'm working with consists of x number of cells, each containing strings (which should really be numbers). I would like to create a non-cell variable that has 1 column, with x number of rows. The main problem is that I can create a variable that has all the strings from the cell array, but since some digits are single and some are double, the rows do not necessarily correspond to the original cells. How can I get the contents of each cell to = a row in a new variable?...

HELP: How to combine two cell arrays into one cell array?
I have two cell arrays. I can not figure out how to get them combined into one simple cell array. In the world of OOP, wouldn't it be nice if you could just do it this way. S3 = S1 + S2; % C++ wishful thinking % How can I do this in MATLAB S1 = { 'Why', 'do', 'some', 'things' }; S2 = {'have', 'to', 'be', 'so', 'difficult', 'in', 'MATLAB?'}; desired result S3 = {'Why', 'do', 'some', 'things' , 'have', 'to', 'be', 'so', 'difficult...

Concatenate cell array of characters & cell array of spaces.
Hi, I need help. I'd like to add a whitespace at the end of each element of a cell array of characters. A = {'A','BC','DEF'}; I'd like to have the following result without any loop. A={'A ','BC ','DEF '}; Thank you in advance, David. "David Dianand" <ddianand@ren.com> wrote in message news:eed22ab.-1@webx.raydaftYaTP... > Hi, I need help. > > I'd like to add a whitespace at the end of each element of a cell > array of characters. > > A = {'A','BC','...

vectorizing cell arrays; making cell arrays faster; for loops
I understand that typically "vectorizing" and removing for-loops makes Matlab code (within Matlab, I'm not talking about compiling/hand-converting to C, C++, etc.) faster inside the Matlab application. However, I have successfully "vectorized" some code involving "cell arrays" and it appears not to be significantly faster, it even may be somewhat slower. Is this to be anticipated? Is there a general paradigm for speeding up cell array operations? Is a double-for loop the best way to do this? Thanks, Sincerely, Joseph PhD and Matlab "exper...

Cell arrays-problem Getting values out of the cell array and using it
Dear all, i have a pbm with the cell array and i do not know how to let matlab extract the value of my variable, 'repetitions' from the cell array.pls advise me. Below is the snipplet of the code where it failed. %Open diaglou box for user to input number of repetitions needed to compute the result prompt = 'Enter the number of repetitions needed to calulate the average error rates:'; title = 'Input for number of repetitions'; num_lines = 1; default = {'10'}; repetitions = inputdlg(prompt, title, num_lines, default); %some mistakes here,not sure %Check to see...

Error: cannot support cell arrays containing cell arrays or objects
I created a cell array from an asc file. I want to convert it to a numeric array to make operations easier. I'm getting the error message: cannot support cell arrays containing cell arrays or objects. How can I fix this? Any help would be much appreciated. for p = 1:length(sample); eval(['fid=fopen(''../RawData/' sample(p).name '@' sample(p).acqo '.asc'');']); d = fgetl(fid); % defines d as the first line of the file date = d(end-8:end); % defines date as the 8th character from the end to the end t = fgetl(fid); %defines...

Cell Array: 'Double' and 'Cell' type sub-arrays
Howdy! I'm a NooB, but am making strides. This is my first line casted to MATLAB Central! I am presented with the following 1x45 cell array: [double] [double] [double] {cell} [double] ... [double] -- note the curly brackets around {cell}...why is that? Every sub-array is a 1000x1 column vector. Cells 1-3, and 5-45 all contain numeric data. Cell 4, on the other hand, contains #NAME? and seems to be interpreted as a cell. *** I would like to convert this cell array to a 1000x45 matrix. If all the sub-arrays were [doubles], then the function CELL2MAT would work perfectly. How...

convert multi-element cell array to single cell array element
I wish to efficiently convert a number of elements contained in a cell array to a single cell array element using carriage returns to preserve the presentation of the contents. I know I can write a bunch of code to achieve this but was hoping for slicker way. Thanks. -Chris for example, given ans as follows, ans = '' 'x < 0.0' 'T' '-' '' 'Actions' 'A1' 'A2' >> whos ans Name Size Bytes Class ans 2x4 520 cell array I...

How to delete row of empty cell arrays without reshaping of cell array?
Hi, I am able to detect empty cells in a cell array. After deleting these cells the cell itself reshapes. That's bad. But I can't figure out how to delete whole rows of empty cells without reshaping of the original cell. Would be nice if somebody could give me a solution. I am sorry. Kind regards, Gudrun In article <28303939.1204057966993.JavaMail.jakarta@nitrogen.mathforum.org>, bluesaturn[at]kellnerweg.de <bluesaturn@kellnerweg.de> wrote: > >I am able to detect empty cells in a cell array. After deleting these cells the cell itself reshapes. >That's bad...