2

I'm trying to find values in a multidimensional array which are only in one column. I can find the correct values when searching the entire multidimensional array. But if I try and limit the find in the multidimensional array to say just the second column the values are not the expected ones.

example of code and correct output:

A = [2 4 6; 8 10 12]
A(:,:,2) = [5 7 9; 11 13 15]
A(:,:,3) = [55 4 55; 167 167 154]

[row,col,page]=ind2sub(size(A), find(A(:,1,:)==4))

row =1,1
col =2,2
page =1,3

But If I try and limit the find to just the second column using these commands

[row,col,page]=ind2sub(size(A), find(A(:,2,:)==4)) 

row =1,1
col =1,3
page =1,1

I get values that are different from the expected ones. I'm trying to limit the multidimensional find to search all pages, all rows, and one specific column. The output should be the same as the first example. How can I fix this?

1
  • 2
    From what I have understood, you are looking to basically find the row and page numbers, when searching along a specific column. Thus, you can focus on finding just the row and page numbers. You can use this - [row,~,page]=ind2sub([size(A,1) 1 size(A,3)],find(A(:,col_num,:)==4)), where col_num would be the column number that you are searching along. The trick is to use 1 instead of size(A,2). Commented Jun 16, 2014 at 17:03

2 Answers 2

3

With [m,n,o]=size(A), you are using A(:,2,:)==4 which is a matrix n*1*o. ind2sub expects a n*m*o matrix. Typical approach is using a mask:

mask=false(size(A));
mask(:,2,:)=true;
[i,j,k]=ind2sub(size(A), find((A(:,:,:)==4)&mask))

The mask selects the entries you are interested in.

Sign up to request clarification or add additional context in comments.

Comments

1

Daniel's answer works by defining a mask and then searching throughout the whole array with that mask applied. The following limits the search to the desired column, and thus may be faster for large arrays:

col = 2; %// desired column
[row, page] = ind2sub([size(A,1) size(A,3)], find(A(:,col,:)==4));

If you need col as a vector the same size as row and page, you can of course achieve it with col = repmat(col,size(row)).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.