Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

add numbers to a matrix/list

6,603 views
Skip to first unread message

Jack Haley

unread,
Jul 6, 2011, 5:09:35 AM7/6/11
to
Hi, I'm very new to Scilab, so am not very familiar with the syntax,
and am finding tutorials very tricky to follow when I need something
quite specific.

I'm familiar with python, however. What I want to do is create an
empty list [] and put numbers in it. In python, this is
straightforward,
a=[]
a.append(1)
print a

a=[1]

etc

easy stuff!
What is the Scilab equivalent?

Tim Wescott

unread,
Jul 6, 2011, 9:01:17 PM7/6/11
to

For arrays, it's:

a = [];
a(1) = 1;
a(2) = 2;
etc.

Or

a = [];
a($+1) = 1;
a($+1) = 2;

or

a = [];
a = [a; 1];
a = [a; 2];

Note that all of these can be slow: when Scilab appends a number to an
array it has to allocate memory for a bigger array, copy, then shove in
the number. If you know beforehand how big the array is going to be,
it's best to preallocate it:

a = zeros(big, 1);
a(1) = this;
a(2) = that;

--

Tim Wescott
Wescott Design Services
http://www.wescottdesign.com

Do you need to implement control loops in software?
"Applied Control Theory for Embedded Systems" was written for you.
See details at http://www.wescottdesign.com/actfes/actfes.html

Philipp Mühlmann

unread,
Oct 28, 2013, 5:27:02 PM10/28/13
to
Hi all,

I'm kind ofnewby with lists and structurs..so I have really difficultnesses to understand the help about "list", "tlist" etc.

What do I want to do?

- get X-Y-coordinates by clicking into an image (use [b,x,y] = xclick()
- for each click write Nr_click, X_click,Y_click into a list

What I think about looks like this:

pos = tlist('typelist','Nr,'X','Y');
for i = 1:nr_clicks
pos.Nr(i) = i;
pos.X(i) = x;
pos.Y(i) = y;
end;

somehow I don't get it work.
Any ideas will be appreachiated.
Thanks,
Philipp

sltan...@gmail.com

unread,
Oct 31, 2013, 1:22:58 AM10/31/13
to
list & tlist are quite similar.
tlist allows you to refer to your list element by name. while list can't.

aList = list('My Custom Type', 1, 20, 0);
aTlist = tlist(['My Custom TList', 'Nr', 'X', 'Y'], 1, 20, 0)

//here's how list display the Nr, X & Y coordinates
aList(2) --> display 1
aList(3) --> display 20
aList(4) --> display 0

//here's how tlist display the Nr, X & Y coordinates
aTlist.Nr --> display 1
aTlist.X --> display 20
aTlist.Y --> display 0
aTlist(2) --> display 1
aTlist(3) --> display 20
aTlist(4) --> display 0


If you want to work with an array of list or tlist, here is the trick.
Take you have a matrix M with r x c dimension
You can't assign M(1) = aTlist;
Instad aTlist.Nr = M(:,1); //you are assigning col-1 of M to Nr
Instad aTlist.X = M(:,2); //you are assigning col-2 of M to Nr
Instad aTlist.Y = M(:,3); //you are assigning col-2 of M to Nr

All the best.

reite...@gmail.com

unread,
Feb 20, 2014, 12:26:29 PM2/20/14
to
Hi,

Apologies for asking specific details, but does anyone know how to to fill up a pre-allocated matrix with column vectors (row num. are the same...)

I keep getting submatrix incorrectly defined error...my piece of code is:


for j=1:numpcs do
t=X(:,1);//pick the score vector (first column of X)
t_pre=t-t;
while (norm(t-t_pre)) > threshold do
p=(X'*t)/(norm(X'*t));//calculate and normalise the loading vector
t_pre=t;//saving the original vector into t_pre
t=X*p;//calculating the new score vector (product of eigenvalue and eigenvector)
else
end;
//extracting components explored
X=X-t*p';//calculating the residual matrix




T[:,j]=t;-------->this is where it stops
P[:,j]=p;-------->this is where it stops
j=1+1;
end
T(:,j+1:numpcs)=[];
P(:,j+1:numpcs)=[];
S=diag(T'*T);
0 new messages