Home > Archive > Matlab > January 2008 > What does [;] mean in Matlab?
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
What does [;] mean in Matlab?
|
|
| thampw@hotmail.com 2008-01-28, 9:41 am |
| Hi,
I'm going through some Matlab codes and I'm wondering what does the a
= a = [a; y(i)] do in the code below. y is an array.
Code:
a= [];
for i = 1:n
if y(i) > 0
a = [a; y(i)]; <== what does this do?
end
end
Thank you.
| |
| Ingo Schupp 2008-01-28, 9:41 am |
| Hello thampw,
with [a;b], you can concatenate matrices or cells
vertically if they have the same number of columns.
Respectively, you can concatenate them horizontally with [a
b].
Example:[color=darkred]
A =
1 2
3 4
[color=darkred]
B =
5 6
[color=darkred]
ans =
1 2
3 4
5 6
Douba
| |
|
| "thampw@hotmail.com" <thampw@hotmail.com> wrote in message
<4b517988-3752-4367-aff9-f08611cb87fe@s27g2000prg.googlegroups.com>...
> Hi,
>
> I'm going through some Matlab codes and I'm wondering what
does the a
> = a = [a; y(i)] do in the code below. y is an array.
>
> Code:
> a= [];
> for i = 1:n
> if y(i) > 0
> a = [a; y(i)]; <== what does this do?
> end
> end
>
> Thank you.
The first semicolon concatenates data row-wise
a = [1 2]
versus
b = [1 ; 2]
The second semi-colon supppresses the output.
[color=darkred]
versus[color=darkred]
hth
Jos
| |
| thampw@hotmail.com 2008-01-28, 9:41 am |
| Thanks Douba.
So, in the case where a = [a; y(i)] and a = [], it would simply mean a
= y(1) in the first loop, and subsequent loop will have a = [y(1);
y(2)] and so forth?
PW
| |
|
| "thampw@hotmail.com" <thampw@hotmail.com> wrote in message
<4b517988-3752-4367-aff9-
f08611cb87fe@s27g2000prg.googlegroups.com>...
> Hi,
>
> I'm going through some Matlab codes and I'm wondering
what does the a
> = a = [a; y(i)] do in the code below. y is an array.
>
> Code:
> a= [];
> for i = 1:n
> if y(i) > 0
> a = [a; y(i)]; <== what does this
do?
> end
> end
>
> Thank you.
Oh, Oh, an easy one for Monday morning! I can even answer
this one!!
the final ; on the assignment line means to not display
the assigned value in the output window. you typically
want to add this in scripts so you don't get a bunch of
outputs to the screen that you don't need.
now the one inside the [] is different, that means start a
new row in the array.
| |
| thampw@hotmail.com 2008-01-28, 9:41 am |
| Thanks all.... I finally understood the code.. 8)
| |
| James Tursa 2008-01-28, 8:20 pm |
| "thampw@hotmail.com" <thampw@hotmail.com> wrote in message
<4b517988-3752-4367-aff9-
f08611cb87fe@s27g2000prg.googlegroups.com>...
> Hi,
>
> I'm going through some Matlab codes and I'm wondering
what does the a
> = a = [a; y(i)] do in the code below. y is an array.
>
> Code:
> a= [];
> for i = 1:n
> if y(i) > 0
> a = [a; y(i)]; <== what does this
do?
> end
> end
>
> Thank you.
Although you already received the answer to your basic
question from others, I have to comment on this code.
1)
It is very bad from an efficiency standpoint to build up a
vector in a loop like this. The "a" variable starts out
empty. Then you create a new version with y(i) added to
the end, then you do this over and over again. The problem
is every time you execute "a = [a; y(i)]" you are forcing
MATLAB to copy the entire contents of the current "a" to a
new variable that is slightly larger, and then adding y
(i). The bulk of your processing time is spent copying the
same data repeatedly over and over again, very little of
the processing time is actually spent doing the real work.
A much better method is to preallocate memory ahead of
time and then fill the preallocated variable inside the
loop. For example, consider the following code:
n = 100000;
y = rand(n,1) - 0.5;
%\
% 1st version, building answer up in a loop
%/
tic
a = [];
for k=1:n
if y(k) > 0
a = [a; y(k)];
end
end
toc
%\
% 2nd version, preallocating, 900 x faster than 1st version
%/
tic
b = zeros(size(y));
m = 0;
for k=1:n
if y(k) > 0
m = m + 1;
b(m) = y(k);
end
end
b = b(1:m);
toc
%\
% show that answers are the same
%/
isequal(a,b)
%\
% 3rd version, one liner, 170 x faster than 1st version
%/
tic
c = y(y>0);
toc
%\
% show that answers are the same
%/
isequal(a,c)
The 2nd version above preallocates memory ahead of time,
thus avoiding all of the recopying of the same data over
and over again. Since you don't know ahead of time how
large you need to make the variable, simply allocate the
max you will need and then cut it down to size at the end.
The one-liner 3rd example is just showing you a compact
way to code this particular example, where the logical
result of y>0 is used as an index to pick off the values
you want. Not quite as fast as the 2nd version, but still
a vast improvement over the 1st version.
2)
Your original code used "i" as an index variable. This
should generally be avoided since MATLAB uses "i" and "j"
for the imaginary number sqrt(-1). That is why I switched
to k.
James Tursa
| |
| James Tursa 2008-01-29, 8:14 pm |
| Correction on the timing tests. I must have slipped a digit
in my original test. The one-liner 3rd version is the
fastest, about 1700 x faster than the 1st version (vice 170
x faster in my previous post).
James Tursa
| |
| Mike Karr 2008-01-31, 10:18 am |
| thampw@hotmail.com wrote:
> Hi,
>
> I'm going through some Matlab codes and I'm wondering what does the a
> = a = [a; y(i)] do in the code below. y is an array.
>
> Code:
> a= [];
> for i = 1:n
> if y(i) > 0
> a = [a; y(i)]; <== what does this do?
> end
> end
>
> Thank you.
I realize your original question has been answered, and that the code
has been analyzed from the point of view of efficiency. But I'll add
a tip that might be useful in the future:
[color=darkred]
hth,
mike
|
|
|
|
|