Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 644859
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T21:26:21+00:00 2026-05-13T21:26:21+00:00

I am trying to sub-array in MATLAB with no luck. This is what I

  • 0

I am trying to sub-array in MATLAB with no luck.

This is what I am doing:

a = randint(latticeSize, 1, [0 1]);

% a 1st attempt which works but sucks 
localPattern = [a(i-1) a(i) a(i+1)];

The above works fine but I want to generalize it with something like:

% this is how I'd like to do it as more general
localPattern = a(i-1 : i+1);

Is there any difference between the two? A display shows the same result but if I use the different alternatives in my code I get very different results (I get what I want with the 1st one).

In case the rest of the code is needed I can provide it, but if someone can spot something just looking at the above then there’s no need.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T21:26:21+00:00Added an answer on May 13, 2026 at 9:26 pm

    Remember: in Matlab (almost) everything is a matrix and has at least two dimensions, even if some of them are “singleton” dimensions. In your case,

    [a(i-1) a(i) a(i+1)]
    

    is a row, and

    a(i-1 : i+1)
    

    is a column in your case, since a is a column. To get the same results in both cases, you can use

    a = randint(1, latticeSize, [0 1]);
    

    or transpose the column

    localPattern = a(i-1 : i+1)';
    

    depending on what goes on in the rest of your code.

    Generally, [] will concatenate things horizontally, and indexing () will keep the dimensions’ “directions” as they were.

    You can run this:

    a = rand(10, 1) 
    i=3 
    localPattern = [a(i-1) a(i) a(i+1)] 
    localPattern = a(i-1 : i+1) 
    

    and take a look at the output — this should clarify things.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to loop through a sub array (which is part of a multidimensional
Hi I have this PHP array of Amazon Sub Categories what I am trying
I'm trying to use sub-domains in my ASP.NET website but I'm coming across a
I am trying to find a sub-string in a string, but I am not
I'm trying to move array items into a sub-array based on a value, so
I'm trying to store an array value so that I can reuse when Sub
I am trying to build a 3D Javascript array, but I am unsure of
I am trying to loop through only a specific sub array in PHP with
I am trying to insert an array into SQL with no luck. I get
I'm trying to add elements to a sub array of the sub array where

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.