Question about square brackets and parentheses in MATLAB.

  • Thread starter Thread starter geft
  • Start date Start date
  • Tags Tags
    Matlab Square
Click For Summary

Discussion Overview

The discussion revolves around the use of square brackets and parentheses in MATLAB, particularly in the context of array and matrix manipulation. Participants explore how these constructs function in various code snippets, addressing both theoretical understanding and practical coding issues.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Homework-related
  • Mathematical reasoning

Main Points Raised

  • One participant inquires about the purpose of square brackets in MATLAB, questioning their role in forming arrays or matrices.
  • Another participant confirms that square brackets are used to create arrays and suggests that they may not be strictly necessary in certain contexts.
  • There is a discussion about the use of parentheses in logical expressions to ensure proper evaluation order, with one participant expressing uncertainty about operator precedence.
  • Some participants note that equality checks can be performed without conditional statements, illustrating this with examples of vector operations.
  • A participant raises a concern about appending to an array element by element, suggesting that this method may be inefficient compared to vectorized operations.
  • Another participant asks about assigning a 1x6 array to a 100x6 matrix within a loop, expressing confusion over dimension mismatch errors and seeking clarification on proper syntax for matrix assignment.
  • One participant provides an example of how to construct matrices using square brackets, emphasizing the importance of using commas and semicolons for column and row separation.
  • There are mentions of preallocation warnings and errors related to matrix dimensions, with participants sharing their experiences and solutions.

Areas of Agreement / Disagreement

Participants generally agree on the basic functionality of square brackets for array creation, but there are varying opinions on the necessity of their use in specific cases. The discussion remains unresolved regarding the best practices for efficient array manipulation and the handling of dimension mismatch errors.

Contextual Notes

Participants express uncertainty about operator precedence in MATLAB and the implications of using different methods for array construction. There are references to specific error messages encountered, indicating potential misunderstandings of MATLAB's array handling.

Who May Find This Useful

This discussion may be useful for MATLAB users, particularly those new to the language or those encountering issues with array and matrix operations. It could also benefit individuals looking to understand logical operations and array manipulations in programming contexts.

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

  • · Replies 6 ·
Replies
6
Views
5K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 13 ·
Replies
13
Views
5K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 7 ·
Replies
7
Views
2K
Replies
15
Views
3K