• Support PF! Buy your school textbooks, materials and every day products Here!

Question about square brackets and parentheses in MATLAB.

  • Thread starter geft
  • Start date
  • #1
148
0
I'm trying to figure out what these do in certain implementations. I can't seem to find the answer in the documentation.
http://www.mathworks.com/matlabcentral/fileexchange/30580-binary-amplitude-shift-keying[1]
Code:
for ii = 1:1:length(bit_stream)

ASK_signal = [ASK_signal (bit_stream(ii)==0)*A1*sin(2*pi*f*t)+...
    (bit_stream(ii)==1)*A2*sin(2*pi*f*t)];
What are the square brackets for and what does ASK_signal within them do? Also, why is bit_stream(i)==0 in parentheses?
Code:
time = [time t];
What does the square brackets do? Do they create an array or something?
Code:
t =  t + 1;
end
Many thanks for the help.
 

Answers and Replies

  • #2
uart
Science Advisor
2,776
9
Yes the square brackets are used to form an array or matrix. Strictly speaking I don't think they are required in the "ASKsignal = ..." line.

Also, why is bit_stream(i)==0 in parentheses?
Very simply, to make sure that this logical expression is evaluated before the other numerical operations. I'm not 100% sure on what exactly is the precedence of those operators, but when in doubt you just use parenthesis.

Assuming that "bitstream" is a vector containing zeros where you want amplitude A1, and ones where you want amplitude A2, then the following code would be how I'd do it.

Code:
ASK_signal = A1*(bit_stream==0).*sin(2*pi*f*t) + A2*bit_stream.*sin(2*pi*f*t);
 
Last edited:
  • #3
148
0
I never knew you could do equality checks without IF ELSE. Thanks for the help.
 
  • #4
uart
Science Advisor
2,776
9
I never knew you could do equality checks without IF ELSE. Thanks for the help.
Yes, if "bitstream" is a vector then (bitstream == 0) is a vector of the same size, containing ones wherever bitstream contains zero, and containing zeros elsewhere.

For example, say that x is [1, 2, 3, 0, 0, -1, 4], then (x==0) will return [0, 0, 0, 1, 1, 0, 0].
 
  • #5
148
0
Since in this case bitstream(ii) is used, I suppose it should return a single value and not an array?

I just realized that this is no conditional statement. That explains why I couldn't get the result I wanted.
 
  • #6
uart
Science Advisor
2,776
9
Since in this case bitstream(ii) is used, I suppose it should return a single value and not an array?

I just realized that this is no conditional statement. That explains why I couldn't get the result I wanted.
Yes your code is appending to the ASKsignal vector, element by element. The code I posted in the first reply above is a much more efficient way to do it.

Basically your code is building up the array term by term. Kind of like the following simple example if you know what I mean (and if you don't, then just try typing it in at the matlab command prompt).

> x = [1];
> x = [x, 0];
> x = [x, 2];

The above example builds up the array x=[1,0,2] term by term.
 
  • #7
148
0
So basically it's just a simple concatenation. I had trouble reading it because that seems rather awkward. It's not my code though, I had the source in the original post.

I have one last question if you don't mind; how do I assign an array to a matrix?

Let's say X is a 1x6 array and I have declared N to be a 100x6 matrix. X is calculated 100 times using a FOR loop. I want X to fill matrix N but I can't seem to do it. I thought N(i,:) would do the trick (i is the loop counter) but it keeps giving me the error "Subscripted assignment dimension mismatch.".

Also, I keep getting the warning: Input arguments must be scalar.

I hope I have provided enough info. Many thanks for the great answers.
 
  • #8
uart
Science Advisor
2,776
9
So basically it's just a simple concatenation. I had trouble reading it because that seems rather awkward. It's not my code though, I had the source in the original post.

I have one last question if you don't mind; how do I assign an array to a matrix?

Let's say X is a 1x6 array and I have declared N to be a 100x6 matrix. X is calculated 100 times using a FOR loop. I want X to fill matrix N but I can't seem to do it. I thought N(i,:) would do the trick (i is the loop counter) but it keeps giving me the error "Subscripted assignment dimension mismatch.".

Also, I keep getting the warning: Input arguments must be scalar.

I hope I have provided enough info. Many thanks for the great answers.
With the "[]" construct, use coma "," (or space) to separate columns and semicolon ";" to separate rows. So you append a new row using ";".

For example

> x = [11, 12, 13, 14, 15, 16];
> x = [x; 21, 22, 23, 24, 25, 26];

makes a 2x6 matrix. You can repeat this process to make your 1000x6 matrix.
 
Last edited:
  • #9
148
0
Ah, I see that I've made another elementary mistake. I really should start getting used to MATLAB. Thanks!
 
  • #10
148
0
Finally I got it to work! I wish I could get rid of the preallocation warnings for time, FSKsignal and Dsignal though. Replacing them with your code doesn't work because Btime is an array, resulting in "Matrix dimensions must agree" error.

Code:
clear all;

% Number of bits
Nbits = 30;
bitstream = round(rand(1,Nbits));

% Carrier modulation
fc1 = 5;
fc2 = 10;
Ac = 5;

% Sampling rate
fs = 100;

% Time per bit
Btime = 0:1/fs:1;

time = [];
FSKsignal = [];
Dsignal = [];

for i = 1:Nbits
    
    FSKsignal = [FSKsignal (bitstream(i)==0)*Ac*sin(2*pi*fc1*Btime)...
    + (bitstream(i)==1)*Ac*sin(2*pi*fc2*Btime)];

    Dsignal = [Dsignal (bitstream(i)==0)*zeros(1,length(Btime))...
    + (bitstream(i)==1)*ones(1,length(Btime))];

    time = [time Btime];
    Btime =  Btime + 1;

end;
 

Related Threads for: Question about square brackets and parentheses in MATLAB.

  • Last Post
Replies
6
Views
2K
  • Last Post
Replies
4
Views
660
  • Last Post
Replies
2
Views
2K
Replies
1
Views
2K
  • Last Post
Replies
9
Views
2K
Replies
2
Views
1K
  • Last Post
Replies
0
Views
975
Replies
3
Views
521
Top