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.
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,
is a row, and
is a column in your case, since
ais a column. To get the same results in both cases, you can useor transpose the column
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:
and take a look at the output — this should clarify things.