Possible to find if a portion of an array equals an array of the same size as the portion?
1 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
采纳的回答
Prannay Jain
2017-4-7
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.
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrices and Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!