return row index of values greater than 0 to a 3 dimensional array

조회 수: 24 (최근 30일)
Charles
Charles 2019년 4월 21일
답변: Andrei Bobrov 2019년 4월 21일
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.
  댓글 수: 7
Charles
Charles 2019년 4월 21일
i have updated the code somewhat. I hope its clearer.
Yes i understaqdn the page 2 is longer. Note sure what the answet is. Perhaps I can fill with zeros to make all the same length, but not sure how to do this
Charles
Charles 2019년 4월 21일
i also want to store the output in the output matrix and i wonder if i have the last line of the code
correct,s o that is store all indices and not overtirwe anything
outputMatrix=columns;

댓글을 달려면 로그인하십시오.

채택된 답변

Image Analyst
Image Analyst 2019년 4월 21일
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)
  댓글 수: 4
Charles
Charles 2019년 4월 21일
편집: Charles 2019년 4월 21일
Hi Thanks for this. Your correct, but what I am seeking as output is merely the column index for elements > 0. in each row vector
Thus I am expecting is
1, 4
1,2,4
2, 4
2, 4
2, 4
i now know that in row 1 i have columns 1 and 4,
in row two, i have columns 1, 2, and 4 etc etc
I guress your output if correct afterall. I need to savre these answer in the outout matrix. how do I do that?
Charles
Charles 2019년 4월 21일
Output E, does represent the column indices you have prodcued with your answer, so your correct. i would like to fit the row and columns into the output matrix, so i I can use it to index into another matrix. How do i pllace tyhe indcies into the output matrix?

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Andrei Bobrov
Andrei Bobrov 2019년 4월 21일
[ii,jj] = find(A > 0);
out = accumarray(ii,jj,[],@(x){sort(x)'});
out{:}

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by