Column index search based on content in cell array

I am trying to figure out how to get the column index when setting two row values ​​in an array of cells with two rows.

I do not know why it is difficult for me to understand this, because you can easily find the value of 1 column.

For example, specify an array of cells below:

{1,1,1,2,2,2;'apple','banana','orange','apple','banana','orange'}.'

I want to find where column1 = 2and column2 ='banana'

The output should be 5.

How can I do it?

+4
source share
2 answers

I will take an array of cells according to @LuisMendo

cellarray = {1,1,1,2,2,2;'apple','banana','orange','apple','banana','orange'}.';
values = cell2mat(cellarray(:,1));
tmp1 = values == 1;
tmp2 = strcmp('banana', cellarray(:,2));
tmp3 = tmp1+tmp2;
result = find(tmp3 == 2);

, , 1. strcmp, 'banana' , . , , , .. tmp3 2.

Whoohoo! . 59, 51 !

find(((([A{:,1}])==1)'+strcmp('banana',A(:,2)))==2)
 =
         2
+4

code-golfing -

find([A{1,:}]==2 & ismember(A(2,:),'banana'))

-

>> A
A = 
    [    1]    [     1]    [     1]    [    2]    [     2]    [     2]
    'apple'    'banana'    'orange'    'apple'    'banana'    'orange'
>> find([A{1,:}]==2 & ismember(A(2,:),'banana'))
ans =
     5
+1

Source: https://habr.com/ru/post/1607499/


All Articles