Creating a 2-D matrix from a 1-D Array?

조회 수: 50 (최근 30일)
Sabarinathan Vadivelu
Sabarinathan Vadivelu 2013년 7월 5일
Considering that I have an array of A = [150 11 12 134 130 152]. How to replicate these elements in this array to get a matrix of size 256*256.

채택된 답변

Matthew Eicholtz
Matthew Eicholtz 2013년 7월 5일
Based on your comment to David's answer, how about this?
r = 256; % number of desired rows
c = 256; % number of desired columns
Avec = repmat(A(:),ceil(r*c/length(A)),1); % vector of repeated A values
Anew = reshape(Avec(1:r*c),c,r)'; % new 2-D matrix of size rxc

추가 답변 (2개)

David Sanchez
David Sanchez 2013년 7월 5일
You do not make clear how you want to replicate your data, but this is a way:
A = [150 11 12 134 130 152];
L = numel(A); % number of elelments in A
M = zeros(256); % initialize the matrix
% assigin values to first row
for k = 0:floor(256/L)
M( 1 , (k*L+1):((k+1)*L) ) = A;
end
% make sure you only have your 256 columns
M = M(:,1:256);
% copy first row to the rest of rows
for k=2:256
M(k,:) = M(1,:);
end
  댓글 수: 1
Sabarinathan Vadivelu
Sabarinathan Vadivelu 2013년 7월 5일
The overflowed elements in the previous rows should start from the next row.
for example,
150 11 12 134 130 152 150 11 12
134 130 150 11 12 134 130 152 11
12 . . . . . . .
. . . . . .
. . . . . .

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


Andrei Bobrov
Andrei Bobrov 2013년 7월 5일
편집: Andrei Bobrov 2013년 7월 5일
out = A(randi(numel(A),256,256));
or
m = 256;
n = 256;
A1 = repmat(A,1,ceil((m+n-1)/numel(A)));
out = hankel(A1(1:m),A1(m:m+n));

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by