How to get a matrix with different row lengths?

I have a two dimensional matrix "check" of zeros and ones. I need to check the ones in each row, take the indexes of ones and store in another matrix. the number of ones are different in each row so the resultant matrix will have different lengths. How do i store them in one matrix?
for i=1:length(NewAps_1)
I2(i,:)=find(check_Naps(i,:))
end
the I2 matrix is the resultant matrix.

 채택된 답변

Ameer Hamza
Ameer Hamza 2018년 5월 1일

0 개 추천

You cannot do that in matrices, because matrix rows need to be equal length. For that, you will need cell array. You can do the following to make cell array
I2 = cell(1, length(NewAps_1));
for i=1:length(NewAps_1)
I2{i}=find(check_Naps(i,:));
end

댓글 수: 4

Sohaib Bin Altaf
Sohaib Bin Altaf 2018년 5월 1일
편집: Sohaib Bin Altaf 2018년 5월 1일
thanks a lot for your reply. But what if i get the required indexes of the ones from each row, and to make the rows of the resultant matrix i.e.I2 equal it adds zeros so the I2 matrix has equal length rows.
like
12 23 33 45 56 70
2 15 40 0 0 0
I may need an extra variable in loop which adjusts the size accordingly.
Yes, you can do that, but it will be messy and a bit difficult to use when you want to again get value for the indexes. Not to mention if a row has more number of indexes than all the previous rows, you will need to expand the entire matrix to make room for new row.
Thanks a lot sir for your help. God bless you.
You are welcome.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

질문:

2018년 5월 1일

댓글:

2018년 5월 1일

Community Treasure Hunt

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

Start Hunting!

Translated by