MATLAB - turning on a function within a domain

Click For Summary

Discussion Overview

The discussion revolves around creating a function in MATLAB to define a piecewise function that outputs 1 for specific values of n (from 0 to 4) and 0 elsewhere. The scope includes technical explanations of MATLAB syntax and function definitions.

Discussion Character

  • Technical explanation
  • Homework-related

Main Points Raised

  • One participant expresses difficulty in creating a function in MATLAB and shares their initial code attempt.
  • Another participant corrects the syntax error, explaining that MATLAB uses parentheses for indexing arrays instead of brackets.
  • A participant seeks clarification on the function definition syntax and its components, indicating they are new to MATLAB.
  • Further explanation is provided regarding the structure of a MATLAB function, including the use of if-else statements and the importance of the "end" keyword.
  • A later reply confirms understanding of the explanation provided and expresses gratitude for the assistance.

Areas of Agreement / Disagreement

Participants generally agree on the correct syntax and structure for defining functions in MATLAB, with no significant disagreements noted.

Contextual Notes

Some limitations may arise from the participant's initial misunderstanding of MATLAB syntax and the requirement for function definitions to be in separate files.

Who May Find This Useful

Individuals learning MATLAB, particularly those new to programming or needing assistance with function definitions and syntax.

Larrytsai
Messages
222
Reaction score
0
Hey guys,

I am stuck trying to create a function in matlab...

x[n] = {1 0 =< n =< 4
{0 elsewhere

so far I have this...

n = [0:4];
x[n] = 1;

but I get this error:

? x[n] = 1;
|
Error: Unbalanced or unexpected parenthesis or bracket.

and I can't figure out what is wrong.

thanks for your help.
 
Physics news on Phys.org
f you are trying to set the n-th element of x to that value, use:
x(n) = 1;

MATLAB syntax for accessing an element of an array is array_name(index) with parens, not brackets.

What I would do is something like:
Code:
function [ Y ] = X(n)
    if (0 <= n) && (n <= 4)
        Y = 1;
    else
        Y = 0;
    end
end

Edit: had some posting problems...
 
Hey thanks for the quick reply.

I am trying to set a interval of n elements to = 1.

Can you please explain "function [ Y ] = X(n)". I don't quite understand. Sorry I am kind of slow at this, as this is my first time breathing with matlab.
 
Okay. In MATLAB, to define a function (this has to be in a separate file, but more on this later), you use the following syntax:
Code:
function [output variables] = function_name(arguments)
    % function code
end
Here, "function" is the key word that tells MATLAB that you are defining a function. In the brackets are the output variables, the values the function gives back after it is run. "function_name" is what you call your function; this is up to you, but the M-file for the function must be named "function_name.m".

Inside the function you put your code. In my example:
Code:
function [ Y ] = X(n)
    if (0 <= n) && (n <= 4)
        Y = 1;
    else
        Y = 0;
    end
end
There is an if block that checks to see if the value of n is between 0 and 4. If it is, based on your original post it returns the value of 1. Otherwise, it returns 0. "&&" means and. Likewise, "||" is or, and "~" is not.

The if-else block and the function block must be closed with an "end" statement.

In your main M-file, to call the function you would just enter:
Code:
% let's say that your input variable is a vector x 
% and you want to store the output to y:
y = X(x);

Does that clear things up?
 
Yeah I understand it now.
thats great thanks a lot for your help!
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
2K
Replies
2
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 13 ·
Replies
13
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K