Inserting a constant array in between each of the coloumns

조회 수: 1 (최근 30일)
Harold Kumar
Harold Kumar 2013년 12월 4일
댓글: Roger Stafford 2013년 12월 4일
Hi All,
Looking for a help on simple problem (New to Matlab)
Question:
I have an (M * N) matrix; now, I want to insert a constant array {1 * N} (say all 1's) in between each of the N columns.
Is there an easy way to do that.
Help is much appreciated.
Thanks a lot.

답변 (1개)

Roger Stafford
Roger Stafford 2013년 12월 4일
You can't place a 1 x N row vector between columns - it wouldn't fit. So I assume you really mean place it between rows. If that is what you mean, do this. Let the M x N matrix be A and the 1 x N array be B.
A(1:2:2*M-1,:) = A;
A(2:2:2*M-2,:) = repmat(B,M-1,1);
If by any chance you really want to place something between the columns, you would need an N x M matrix and an N x 1 constant array, that is, a column vector, and you would have to reverse the columns and rows in the above code.
  댓글 수: 2
Harold Kumar
Harold Kumar 2013년 12월 4일
편집: Harold Kumar 2013년 12월 4일
Hi Roger,
Thanks for responding.
Not to worry. I managed to figure it out.
Here is the answer
----------
If you don't mind creating a temporary matrix, one way to do it would be to do the following:
old_matrix = rand(M,N); % Just for example
new_matrix = ones(M,2*N-1);
new_matrix(:,1:2:end) = old_matrix;
Note that for an arbitrary constant matrix, you could replace the second line with the following:
new_matrix = repmat(const_array,1,2*N-1);
Roger Stafford
Roger Stafford 2013년 12월 4일
You originally asserted that your constant array was 1 x N which is a row vector. I'm glad you changed to an M x 1 column vector here, which would indeed fit between columns.

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

카테고리

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