Question about square brackets and parentheses in MATLAB.

In summary, the conversation discusses the use of square brackets to create arrays or matrices in Matlab, as well as the use of logical expressions to perform equality checks without IF ELSE statements. The code provided also shows how to assign an array to a matrix using the "[]" construct and how to append rows to a matrix using semicolons. Preallocation warnings are mentioned but a solution is provided.
  • #1
geft
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.
 
Physics news on Phys.org
  • #2
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
I never knew you could do equality checks without IF ELSE. Thanks for the help.
 
  • #4
geft said:
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
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
geft said:
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
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
geft said:
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
Ah, I see that I've made another elementary mistake. I really should start getting used to MATLAB. Thanks!
 
  • #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:
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 to Question about square brackets and parentheses in MATLAB.

1. What is the difference between square brackets and parentheses in MATLAB?

Square brackets are used to create arrays or matrices in MATLAB, while parentheses are used for function calls and input arguments.

2. Can I use square brackets and parentheses interchangeably?

No, they have different functions and serve different purposes in MATLAB. Using them interchangeably can result in errors.

3. How do I access specific elements in an array or matrix using square brackets?

You can access specific elements in an array or matrix by using their index values inside the square brackets, separated by a comma. For example, A(1,2) will access the element in the first row and second column of matrix A.

4. Can I use square brackets to perform mathematical operations in MATLAB?

Yes, square brackets can be used to perform mathematical operations on arrays or matrices in MATLAB. In this case, the operations will be performed element-wise.

5. Are square brackets and parentheses used in any other way in MATLAB?

Yes, square brackets can also be used to concatenate arrays or matrices, while parentheses can be used to group operations and change the order of evaluation in an expression.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Advanced Physics Homework Help
Replies
6
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
913
  • Introductory Physics Homework Help
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
Back
Top