From: michael caro on
On May 4, 7:03 pm, Walter Roberson <rober...(a)hushmail.com> wrote:
> michael caro wrote:
> > On May 4, 6:09 pm, Nathan <ngrec...(a)gmail.com> wrote:
> >> If so:
>
> >> for i = 1:na
> >>     Y(i,:)={1./sqrt(1+(t.*a(i)).^2)} %note the braces
> >> end
> > Thank you for picking up on the mistake but thats not exactly what I'm
> > looking for.
> > How can I change the loop so matlab doesnt try to convert it into a
> > double?
>
> Nathan already answered that. Go back and reexamine the line, and "note the
> braces". His solution already prevents matlab from trying to convert into a
> double.
>
> > i'd like to create an array with 5 rows and 101 columns. the 5 rows
> > correspond to each different 'a' variable being evaluated with all of
> > the 't' variables, so i can then create a plot saying plot(t,Y)
>
> If you want to be able to do it with a single plot() call like you show, then
> you *do* want the values converted to double -- plot can handle a numeric Y
> array with multiple rows, but it cannot handle cell arrays to represent the rows.

When I run the script with Nathan's suggestion, this is the output :
Y =

Columns 1 through 5

[1x101 double] [1x101 double] [1x101 double] [1x101
double] [1x101 double]
[1x101 double] [1x101 double] [1x101 double] [1x101
double] [1x101 double]
[1x101 double] [1x101 double] [1x101 double] [1x101
double] [1x101 double]
[1x101 double] [1x101 double] [1x101 double] [1x101
double] [1x101 double]
[1x101 double] [1x101 double] [1x101 double] [1x101
double] [1x101 double]

for all columns.
From: Matt Fig on
You were close.


t=0:0.05:5;
a=0:0.5:2;
Y = zeros(numel(a),numel(t)); % No cell array needed.

for ii = 1:numel(a)
Y(ii,:) = 1./sqrt(1+(a(ii)*t).^2);
end

plot(t,Y')
legend(cellstr(num2str(a')))
From: Nathan on
On May 4, 4:44 pm, michael caro <mcaro1...(a)gmail.com> wrote:
> On May 4, 7:03 pm, Walter Roberson <rober...(a)hushmail.com> wrote:
>
>
>
> > michael caro wrote:
> > > On May 4, 6:09 pm, Nathan <ngrec...(a)gmail.com> wrote:
> > >> If so:
>
> > >> for i = 1:na
> > >>     Y(i,:)={1./sqrt(1+(t.*a(i)).^2)} %note the braces
> > >> end
> > > Thank you for picking up on the mistake but thats not exactly what I'm
> > > looking for.
> > > How can I change the loop so matlab doesnt try to convert it into a
> > > double?
>
> > Nathan already answered that. Go back and reexamine the line, and "note the
> > braces". His solution already prevents matlab from trying to convert into a
> > double.
>
> > > i'd like to create an array with 5 rows and 101 columns. the 5 rows
> > > correspond to each different 'a' variable being evaluated with all of
> > > the 't' variables, so i can then create a plot saying plot(t,Y)
>
> > If you want to be able to do it with a single plot() call like you show, then
> > you *do* want the values converted to double -- plot can handle a numeric Y
> > array with multiple rows, but it cannot handle cell arrays to represent the rows.
>
> When I run the script with Nathan's suggestion, this is the output :
> Y =
>
>   Columns 1 through 5
>
>     [1x101 double]    [1x101 double]    [1x101 double]    [1x101
> double]    [1x101 double]
>     [1x101 double]    [1x101 double]    [1x101 double]    [1x101
> double]    [1x101 double]
>     [1x101 double]    [1x101 double]    [1x101 double]    [1x101
> double]    [1x101 double]
>     [1x101 double]    [1x101 double]    [1x101 double]    [1x101
> double]    [1x101 double]
>     [1x101 double]    [1x101 double]    [1x101 double]    [1x101
> double]    [1x101 double]
>
> for all columns.

Just note: That is precisely what YOUR code does. All I did was get
rid of the cell to double conversion problem.
Also note that you asked for a 5x101 Y cell array, and that is what
you get.

Perhaps you should take a look at your code, go through step by step,
and determine what is wrong.

As Walter suggests, perhaps you DO want a double array Y. In this
case, initialize Y to be a double array, not a cell array.

t=0:0.05:5;
a=0:0.5:2;
nt=numel(t);
na=numel(a);
Y=zeros(na,nt); %allocate array
for i = 1:na
Y(i,:)=1./sqrt(1+(t.*a(i)).^2);
end

-Nathan
From: michael caro on
On May 4, 8:05 pm, Nathan <ngrec...(a)gmail.com> wrote:
> On May 4, 4:44 pm, michael caro <mcaro1...(a)gmail.com> wrote:
>
>
>
> > On May 4, 7:03 pm, Walter Roberson <rober...(a)hushmail.com> wrote:
>
> > > michael caro wrote:
> > > > On May 4, 6:09 pm, Nathan <ngrec...(a)gmail.com> wrote:
> > > >> If so:
>
> > > >> for i = 1:na
> > > >>     Y(i,:)={1./sqrt(1+(t.*a(i)).^2)} %note the braces
> > > >> end
> > > > Thank you for picking up on the mistake but thats not exactly what I'm
> > > > looking for.
> > > > How can I change the loop so matlab doesnt try to convert it into a
> > > > double?
>
> > > Nathan already answered that. Go back and reexamine the line, and "note the
> > > braces". His solution already prevents matlab from trying to convert into a
> > > double.
>
> > > > i'd like to create an array with 5 rows and 101 columns. the 5 rows
> > > > correspond to each different 'a' variable being evaluated with all of
> > > > the 't' variables, so i can then create a plot saying plot(t,Y)
>
> > > If you want to be able to do it with a single plot() call like you show, then
> > > you *do* want the values converted to double -- plot can handle a numeric Y
> > > array with multiple rows, but it cannot handle cell arrays to represent the rows.
>
> > When I run the script with Nathan's suggestion, this is the output :
> > Y =
>
> >   Columns 1 through 5
>
> >     [1x101 double]    [1x101 double]    [1x101 double]    [1x101
> > double]    [1x101 double]
> >     [1x101 double]    [1x101 double]    [1x101 double]    [1x101
> > double]    [1x101 double]
> >     [1x101 double]    [1x101 double]    [1x101 double]    [1x101
> > double]    [1x101 double]
> >     [1x101 double]    [1x101 double]    [1x101 double]    [1x101
> > double]    [1x101 double]
> >     [1x101 double]    [1x101 double]    [1x101 double]    [1x101
> > double]    [1x101 double]
>
> > for all columns.
>
> Just note: That is precisely what YOUR code does. All I did was get
> rid of the cell to double conversion problem.
> Also note that you asked for a 5x101 Y cell array, and that is what
> you get.
>
> Perhaps you should take a look at your code, go through step by step,
> and determine what is wrong.
>
> As Walter suggests, perhaps you DO want a double array Y. In this
> case, initialize Y to be a double array, not a cell array.
>
> t=0:0.05:5;
> a=0:0.5:2;
> nt=numel(t);
> na=numel(a);
> Y=zeros(na,nt); %allocate array
> for i = 1:na
>     Y(i,:)=1./sqrt(1+(t.*a(i)).^2);
> end
>
> -Nathan

Nathan,

I really appreciate your help with this problem. I'm sorry if I seem a
little dense. The code works, your the man!

-Mike
From: Walter Roberson on
michael caro wrote:
> On May 4, 7:03 pm, Walter Roberson <rober...(a)hushmail.com> wrote:

>>> How can I change the loop so matlab doesnt try to convert it into a
>>> double?
>> Nathan already answered that. Go back and reexamine the line, and "note the
>> braces".

> When I run the script with Nathan's suggestion, this is the output :
> Y =
>
> Columns 1 through 5
>
> [1x101 double] [1x101 double] [1x101 double] [1x101
> double] [1x101 double]
> [1x101 double] [1x101 double] [1x101 double] [1x101

> for all columns.

Yup, exactly as I would expect from your code.

You asked to have the Matlab complaint about "Conversion to cell from double
is not possible" fixed, and you asked to have the loop be such that Matlab
doesn't try to convert the value to double, and the code Nathan provided
satisfies both conditions. Clearly the error about converting to cell from
double is gone, is it not? And in the loop, Matlab is not trying to convert
the value to double because it already knows that the value is a cell array,
which is what is required for the assignment into the array of cells, Y, to
succeed, so it doesn't have any reason to convert the computed cell array
value into double. The code is performing according to your specifications.

If you are talking about the fact that Y displays as a bunch of repetitions of
text that mention the word 'double', then Yes, that is how Matlab displays
every cell array entry that contains a double precision array, unless the
double precision array happens to contain only a single 1x1 value, in which
case that one value is displayed inside []. Matlab isn't converting anything
to double there: it is telling you about what is already in the cell array --
the double precision values are already there, without conversion.

If you want Matlab to use a different data type instead of double precision to
represent the values of your graph, then you are going to find you have a lot
of trouble getting a usable graph... unless, that is, you happen to choose
single precision instead. Your code appears likely to generate a large number
of values in the range 0 to 1: if you choose to represent those as any data
type other than double precision or single precision, then Matlab is going to
throw away the fractional parts, probably leaving you with just a bunch of
entries that are each either 0 or 1. You should be able to get a reasonable
graph with single precision -- if you want to give that a try, change the loop
from
Y(i,:)={1./sqrt(1+(t.*a(i)).^2)} %note the braces
to
Y(i,:)={single(1./sqrt(1+(t.*a(i)).^2))} %NOTE THE BRACES ARE STILL THERE

You might as well go ahead and try it: the horse has to be beaten quite to
death before it can be turned into the right kind of glue.


When you are finished with that, you may wish to study my prior comment:

>> If you want to be able to do it with a single plot() call like you show, then
>> you *do* want the values converted to double -- plot can handle a numeric Y
>> array with multiple rows, but it cannot handle cell arrays to represent
the rows.

Your Y array is *not* double, and I already told you that you wouldn't be able
to plot it. So what kind of array *is* Y? And what kind of array do you need
to provide to plot() in order to get multiple rows plotted at once? And how do
you get from what you have now to the kind of array you need?
First  |  Prev  |  Next  |  Last
Pages: 1 2 3
Prev: rgb image processing
Next: neural networks