Creating a tabular - maybe using cell ?

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Creating a tabular - maybe using cell ?

AlbFrigerio
Hello everyone, I'm trying to use cells to create something like a tabular, but I cannot reach it. Here it is my problem, I've got a tabular whose lines are of the form [string string float float integer float] , which is updated in a for loop.

I'd like to create in Octave something like a tabular representing my input, so that I can add a line or access - for example - all the elements in the first column by typing tabular{:,1} .

I tried to make something like

a={}; # a = cell(0,6);
for i=1:5
b={"hello","world",.1 .2 3 .4};
a={a;b};
end

but I don't reach something useful. I could use
a={}; # a = cell(0,6);
for i=1:5
b={"hello","world",.1 .2 3 .4}; # in real example this one changes at every i in the loop
for j=1:6
a{i,j}=b{j};
end
end

but I hope it may exist a faster way!!

Moreover, I'd like the command www = a{:,1} to give me the matrix ["hello" ; "hello" ; "hello" ; "hello" ;"hello"] but I only got the last result.

Thank you so much, have a nice day,

  Alberto
Reply | Threaded
Open this post in threaded view
|

Re: Creating a tabular - maybe using cell ?

Jaroslav Hajek-2
On Tue, Oct 5, 2010 at 4:55 PM, AlbFrigerio
<[hidden email]> wrote:

>
> Hello everyone, I'm trying to use cells to create something like a tabular,
> but I cannot reach it. Here it is my problem, I've got a tabular whose lines
> are of the form [string string float float integer float] , which is updated
> in a for loop.
>
> I'd like to create in Octave something like a tabular representing my input,
> so that I can add a line or access - for example - all the elements in the
> first column by typing tabular{:,1} .
>
> I tried to make something like
>
> a={}; # a = cell(0,6);
> for i=1:5
> b={"hello","world",.1 .2 3 .4};
> a={a;b};
> end
>

You probably want

a = [a;b];

but in general the fastest method will be to either allocate a in
advance or, if the numer of rows is not known, accumulate rows in an
extra array using a{end+1} = b and then do a = vertcat (a{:}) at the
end.

Note that by "fastest" I normally mean the latest development version;
the stable version may give you a diametrally different (and usually
worse) performance.

hth
_______________________________________________
Help-octave mailing list
[hidden email]
https://www-old.cae.wisc.edu/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: Creating a tabular - maybe using cell ?

AlbFrigerio
Thanks Jaroslav, as usual you are completely right :)

Just another question : using the commands you suggested

a={}; # a = cell(0,6);
for i=1:5
b={"hello","world",.1 .2 3 .4};
a=[a;b];
end

I reach the desided output. Let us suppose that I want to delete a row (or a column) of this "tabular" , how should I do?

For example, if I want to delete the first row I might use a{2:end,:} but I don't have a tabular. I might use {a{2:end,:}} , but I get a 1*24 tabular, not a 4*6 as I'd like to.

Thanks again,
   Alberto
Reply | Threaded
Open this post in threaded view
|

Re: Creating a tabular - maybe using cell ?

AlbFrigerio
Solved, I just used wrong parentheses : a(1,:)=[] was the right command to use !!
Reply | Threaded
Open this post in threaded view
|

Re: Creating a tabular - maybe using cell ?

Jaroslav Hajek-2
On Wed, Oct 6, 2010 at 10:32 AM, AlbFrigerio
<[hidden email]> wrote:
>
> Solved, I just used wrong parentheses : a(1,:)=[] was the right command to
> use !!

Yes, trial and error is an option :)
Or you can read the docs to learn the differences between {} and ()
indexing of cells.
_______________________________________________
Help-octave mailing list
[hidden email]
https://www-old.cae.wisc.edu/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: Creating a tabular - maybe using cell ?

AlbFrigerio
Here in Italy we say "Ben detto" , the key fact is that - as usual - you are completely right !!