필터 지우기
필터 지우기

HOW CAN I RE-EDIT MATRIX?

조회 수: 4 (최근 30일)
Fabio
Fabio 2014년 5월 22일
댓글: Fabio 2014년 5월 22일
Hi!
How can "re-edit" this matrix:
[1 3 4;
2 5 7;
3 1 6;
4 2 8]
in this:
[1 4;
1 4;
1 4;
2 7;
2 7;
2 7;
2 7;
2 7;
3 6;
4 8;
4 8]
where each row is replaced for n thimes where n is the second column in the first matrix?
Thank you!
Fabio

채택된 답변

José-Luis
José-Luis 2014년 5월 22일
your_mat = cell2mat(arrayfun(@(n) repmat(a(n,[1 3]),a(n,2),1),(1:size(a,1))','uniformoutput',false));
  댓글 수: 1
Fabio
Fabio 2014년 5월 22일
Thank you

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

추가 답변 (1개)

Geoff Hayes
Geoff Hayes 2014년 5월 22일
First determine the dimensions of the new matrix (called B say) which will mx2 where m is the sum of the middle column. Then iterate over each row of your initial matrix (called A say) and replicate the end points of that row (first and third columns) by the value in the second column:
B(j:j+A(i,2)-1,:) = repmat([A(i,1) A(i,3)],A(i,2),1);
where i is the ith row of A and j is the next row to insert your replicated block (with j being incremented on each iteration). So in the above piece of code, we are replacing all rows in B from j to j+A(i,2)-1 with the ends of the ith row of A replicated A(i,2)x1 times.
Hope that the above helps!
  댓글 수: 1
Fabio
Fabio 2014년 5월 22일
Thank you

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

카테고리

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

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by