
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 preallocated. 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 100831 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 preallocated. 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 preallocated. 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 100831 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 preallocated. 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 100831 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 preallocated. 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 100831 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 100831 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 100831 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



8 Replies
246 Views
Similar Articles
[PageSpeed]
36

Similar Artilces:
for loop to open text files and place in cell array? any adviceI 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 stringsHi
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 arraysHi 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 arrayHallo.
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 cellHey 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 ArrayI 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 best solu... Part of cell array into another cell arrayHello
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 StringsHi,
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... Converting cell array of strings to cell array of numbersI 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...



