Clear Filters
Clear Filters

Possible to find if a portion of an array equals an array of the same size as the portion?

2 views (last 30 days)
I have a school project where I have written a program that creates a simple binary array based on inputs. The output array usually looks something like this:
X = [1 0 0 0 0 1 0 1 0]
I need to write a simple script that will convert this to material type, for which I have the key. Basically, if the last 3 elements of the array ([0 1 0]) equal to [0 1 0], then my material is Red Glass. They could potentially equal 3 other possibilities. If the array elements 2 through 6 ([0 0 0 0 1]) equal [0 0 0 0 1], then my material is type 1 zero and type 2 zero. There are potentially 15 other possibilities of what these specific elements might be. The first element of the array is always 1, and does not matter.
I basically need to write a script that is an IF function that asks if the last 3 elements of the array equal this, this, or that, then I have THIS (and so on), and then do the same thing for elements 2-6, and then pair up the conclusions.

Accepted Answer

Prannay Jain
Prannay Jain on 7 Apr 2017
Yes, it is possible to compare a part of an array with another array of the same size of sub-array.
You will have to write lots of if condition to match the sub-array. For example,
X = [1 0 0 0 0 1 0 1 0];
n = X(7:9);
if(n == [0 1 0])
disp('Red Glass');
output = 'Red Glass'; %%or store it in some other variable
elseif (n == [1 1 1])
disp('Blue Glass')
else
disp('Something else')
end
output
Similarly, you can do for index 2 to 6,
m = X(2:6)
and write if conditions.

More Answers (0)

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!