38 views (last 30 days)

I have a Matrix A, of m x n dimensions.

I wish to go through each row from left to right and return the index of values greater than 0. I have tried the folowing which returns the column index for the first row which has values. for example row 26.

I have tried the following but I am not getting the outputmatrix with all entries. I fear the entries are being overwritten

A =[2,-4,-0.5,0.34;0.01,4,-0.5,0.34;-10,4,-0.2,0.6;-10,4,-0.2,0.6;-19,15,-0.7,0.6];

% Now we have input matrix A we want to return the col index of each row element greater than 0.

% inputMatrix= A

outputMatrix=zeros(1,[],size(A,1));

for i = 26:size(A(2:end,:),1)

for ij = 1:size(A(2:end,:),2)

[rows, columns] = find(inputMatrix > 0)

outputMatrix=columns;

end

end

A =

2 -4 -0.5 0.34

0.01 4 -0.5 0.34

-10 4 -0.2 0.6

-10 4 -0.2 0.6

-19 15 -0.7 0.6

However I wish to go through each row, and store these column indices in a 3 dimensional array where each page represents the set of column indices from each row of A

Example output is a 3 dimensional array of column indices. - Matrix E

1, 4 (page 1)

1, 2, 4 (page 2)

2, 4 (page 3

2, 4 (page 4)

2,4 (page 5)

I then want to use these column indices to return the value from the corresponding rows of a matrix C and matrix D. Matrix C and D are the same dimentions as A

I then want to multiply C values by D values and return a 3 dimension array of results. - Matrix F

The results will be of the same dimension as Matrix E

Any help appreciated.

Image Analyst
on 21 Apr 2019

Try this:

A = [2,-4,-0.5,0.34;0.01,4,-0.5,0.34;-10,4,-0.2,0.6;-10,4,-0.2,0.6;-19,15,-0.7,0.6];

[rows, columns] = find(A > 0)

Image Analyst
on 21 Apr 2019

Explain which of those (row, column) pairs do not represent a non-zero value.

for example A(2,1) is zero when rows(2) and columns(2), which give a (row, column) pair of (2, 1) say that it's non-zero (in fact for this location, A(2,1) is 0.01 which is in fact non-zero)

Are you sure you're thinking in terms of (x,y) and not (row, column)? Matrices are not indexed (x,y) as you probably (or should) know.

In your example up top, explain how you get each element of E.

Sign in to comment.

Andrei Bobrov
on 21 Apr 2019

[ii,jj] = find(A > 0);

out = accumarray(ii,jj,[],@(x){sort(x)'});

out{:}

Sign in to comment.

Sign in to answer this question.

Opportunities for recent engineering grads.

Apply Today
## 7 Comments

## Direct link to this comment

https://uk.mathworks.com/matlabcentral/answers/457648-return-row-index-of-values-greater-than-0-to-a-3-dimensional-array#comment_696444

⋮## Direct link to this comment

https://uk.mathworks.com/matlabcentral/answers/457648-return-row-index-of-values-greater-than-0-to-a-3-dimensional-array#comment_696444

## Direct link to this comment

https://uk.mathworks.com/matlabcentral/answers/457648-return-row-index-of-values-greater-than-0-to-a-3-dimensional-array#comment_696469

⋮## Direct link to this comment

https://uk.mathworks.com/matlabcentral/answers/457648-return-row-index-of-values-greater-than-0-to-a-3-dimensional-array#comment_696469

## Direct link to this comment

https://uk.mathworks.com/matlabcentral/answers/457648-return-row-index-of-values-greater-than-0-to-a-3-dimensional-array#comment_696471

⋮## Direct link to this comment

https://uk.mathworks.com/matlabcentral/answers/457648-return-row-index-of-values-greater-than-0-to-a-3-dimensional-array#comment_696471

## Direct link to this comment

https://uk.mathworks.com/matlabcentral/answers/457648-return-row-index-of-values-greater-than-0-to-a-3-dimensional-array#comment_696501

⋮## Direct link to this comment

https://uk.mathworks.com/matlabcentral/answers/457648-return-row-index-of-values-greater-than-0-to-a-3-dimensional-array#comment_696501

## Direct link to this comment

https://uk.mathworks.com/matlabcentral/answers/457648-return-row-index-of-values-greater-than-0-to-a-3-dimensional-array#comment_696511

⋮## Direct link to this comment

https://uk.mathworks.com/matlabcentral/answers/457648-return-row-index-of-values-greater-than-0-to-a-3-dimensional-array#comment_696511

## Direct link to this comment

https://uk.mathworks.com/matlabcentral/answers/457648-return-row-index-of-values-greater-than-0-to-a-3-dimensional-array#comment_696519

⋮## Direct link to this comment

https://uk.mathworks.com/matlabcentral/answers/457648-return-row-index-of-values-greater-than-0-to-a-3-dimensional-array#comment_696519

## Direct link to this comment

https://uk.mathworks.com/matlabcentral/answers/457648-return-row-index-of-values-greater-than-0-to-a-3-dimensional-array#comment_696520

⋮## Direct link to this comment

https://uk.mathworks.com/matlabcentral/answers/457648-return-row-index-of-values-greater-than-0-to-a-3-dimensional-array#comment_696520

Sign in to comment.