Question about square brackets and parentheses in MATLAB.

  • Thread starter Thread starter geft
  • Start date Start date
  • Tags Tags
    Matlab Square
AI Thread Summary
The discussion focuses on the use of square brackets and parentheses in MATLAB, specifically in the context of constructing arrays and evaluating logical expressions. Square brackets are confirmed to concatenate arrays, while parentheses are used to ensure the correct order of operations in logical comparisons. The original code is critiqued for its inefficiency in building arrays element by element, with suggestions for more efficient vectorized operations. Additionally, a user seeks clarification on assigning a 1x6 array to a 100x6 matrix, encountering dimension mismatch errors, which highlights the importance of proper array dimensions in MATLAB. The conversation concludes with users sharing tips on avoiding common pitfalls in MATLAB coding.
geft
Messages
144
Reaction score
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
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:
I never knew you could do equality checks without IF ELSE. Thanks for the help.
 
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].
 
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.
 
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.
 
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.
 
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:
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;
 

Similar threads

Back
Top