separating element in cell according to some condition

조회 수: 1 (최근 30일)
NA
NA 2020년 4월 18일
편집: NA 2020년 4월 25일
I have A
A={[237;238;241;243], [238;237;241;243;247], [241;237;238;243;247],...
[243;237;238;241;247;310],[247;238;241;243;310;381],[310;243;247],[381;247;310] }
I want to use this algorithm
First cell
  1. A{1}=[237;238;241;243]
  2. Put first and second element of A{1} in temp (temp=[237,238])
  3. Choose last element in temp, if it has only 2 element (238)
  4. Choose cell in A that the first element is 238 (A{2}=[238;237;241;243;247])
  5. Third element in A{1} is 241
  6. Does 241 belong to A{2}? yes
  7. So, we could add 241 to temp (temp=[237,238, 241])
  8. 4th element in A{1} is 243
  9. Does 243 belong to A{2} and A{3}? Yes ---> I have a problem to write a code for this part
  10. So, we could put 243 on temp (temp=[237,238, 241,243])
Result in this cell, temp =[237,238, 241,243])

답변 (1개)

Mehmed Saad
Mehmed Saad 2020년 4월 18일
I will give you a hint for A{1}(4) with A{2} and A{3}
  1. compare 4th element of A{1} with A{2} all element
  2. if any of them is 1 it means it is inside that cell. it will give you output in the form of logical array [0 0 1 0]
  3. compare 4th element of A{1} with A{3} all element, and sum the logical array, if its sum is greater than 0 it means that 4th element of A{1} exist in A{3}. apply and operation on the logical array given by step 2 with current output
  4. feed these logical index to A{2}(logical index)
temp = [temp;A{2}(and((sum(A{3}==A{1}(4))>0),(A{2}==A{1}(4))))];

카테고리

Help CenterFile Exchange에서 Dates and Time에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by