Possible to find if a portion of an array equals an array of the same size as the portion?
조회 수: 5 (최근 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!