1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Homework Help: Question about square brackets and parentheses in MATLAB.

  1. Sep 28, 2012 #1
    I'm trying to figure out what these do in certain implementations. I can't seem to find the answer in the documentation.
    Code (Text):
    for ii = 1:1:length(bit_stream)

    ASK_signal = [ASK_signal (bit_stream(ii)==0)*A1*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 (Text):
    time = [time t];
    What does the square brackets do? Do they create an array or something?
    Code (Text):
    t =  t + 1;
    Many thanks for the help.
  2. jcsd
  3. Sep 28, 2012 #2


    User Avatar
    Science Advisor

    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.

    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 (Text):

    ASK_signal = A1*(bit_stream==0).*sin(2*pi*f*t) + A2*bit_stream.*sin(2*pi*f*t);
    Last edited: Sep 28, 2012
  4. Sep 28, 2012 #3
    I never knew you could do equality checks without IF ELSE. Thanks for the help.
  5. Sep 28, 2012 #4


    User Avatar
    Science Advisor

    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].
  6. Sep 28, 2012 #5
    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.
  7. Sep 28, 2012 #6


    User Avatar
    Science Advisor

    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.
  8. Sep 28, 2012 #7
    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.
  9. Sep 28, 2012 #8


    User Avatar
    Science Advisor

    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: Sep 28, 2012
  10. Sep 28, 2012 #9
    Ah, I see that I've made another elementary mistake. I really should start getting used to MATLAB. Thanks!
  11. Sep 29, 2012 #10
    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 (Text):
    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;

Share this great discussion with others via Reddit, Google+, Twitter, or Facebook