2017-01-03 1 views
2

누락 된 값이 0으로 채워지는 0 또는 숫자가 아닌 과 같이 일치하지 않는 벡터로 구성된 행렬을 입력하려면 어떻게해야합니까?일치하지 않는 벡터로 행렬을 작성하십시오.

은 (물론, 0들의 행렬이 먼저 생성 할 수 있고, 매칭 벡터는 한 라인 씩 첨가 할 수 있지만, 어떤 I이 1 줄 것인지?)

예 :

a = [ 
     1 2 3 4; 
     1 2 0 0; 
     1 0 0 0; 
    ]; 
0 : 결과

a = [ 
     1 2 3 4; 
     1 2  ; 
     1   ; 
    ]; 

: 어떻게 같은 매트릭스를 입력 할 수 있습니다

또는

원하지 않는 솔루션 :

a  = zeros(3,4); 
a(1,1:4) = [1 2 3 4]; 
a(2,1:2) = [1 2 ]; 
a(3,1:1) = [1  ]; 
+2

그 일치하지 않는 벡터가 저장되는 방법/어떻게 그 벡터를 받고 여기에 elemnts 하나의 쉼표로 saparetad해야합니까? – Divakar

+0

그림과 같이 수동 입력. 프로그램이 제로 패드를 처리하고 작성하고 처리하는 것은 매우 쉽지만, 인간 사용자에게는 넓은 0의 배열에서 손실됩니다. – kando

+0

또한 벡터 셀을 만드는 것을 피하기 위해 노력하지만 가능한 해결책이기도합니다. – kando

답변

5

호세 반 데르 기 스트는 매스 웍스의 파일 교환에 대한 인기가 참으로 아주 좋은 유틸리티를 제출 - padcat.

본질적으로 수동으로 제안한 내용을 자동화합니다. 그러나, 일부 스마트 연결 및 인덱싱 트릭을 사용하여 상기 매트릭스를 매우 효율적으로 생성합니다. 당신은 단지 숫자 (0-9)이있는 경우

여기
function [M, TF] = padcat(varargin) 
% PADCAT - concatenate vectors with different lengths by padding with NaN 
% 
% M = PADCAT(V1, V2, V3, ..., VN) concatenates the vectors V1 through VN 
% into one large matrix. All vectors should have the same orientation, 
% that is, they are all row or column vectors. The vectors do not need to 
% have the same lengths, and shorter vectors are padded with NaNs. 
% The size of M is determined by the length of the longest vector. For 
% row vectors, M will be a N-by-MaxL matrix and for column vectors, M 
% will be a MaxL-by-N matrix, where MaxL is the length of the longest 
% vector. 
% 
% Examples: 
%  a = 1:5 ; b = 1:3 ; c = [] ; d = 1:4 ; 
%  padcat(a,b,c,d) % row vectors 
%   % -> 1  2  3  4  5 
%   %  1  2  3 NaN NaN 
%   % NaN NaN NaN NaN NaN 
%   %  1  2  3  4 NaN 
%  CC = {d.' a.' c.' b.' d.'} ; 
%  padcat(CC{:}) % column vectors 
%   %  1  1 NaN  1  1 
%   %  2  2 NaN  2  2 
%   %  3  3 NaN  3  3 
%   %  4  4 NaN NaN  4 
%   % NaN  5 NaN NaN NaN 
% 
% [M, TF] = PADCAT(..) will also return a logical matrix TF with the same 
% size as R having true values for those positions that originate from an 
% input vector. This may be useful if any of the vectors contain NaNs. 
% 
% Example: 
%  a = 1:3 ; b = [] ; c = [1 NaN] ; 
%  [M,tf] = padcat(a,b,c) 
%  % find the original NaN 
%  [Vev,Pos] = find(tf & isnan(M)) 
%  % -> Vec = 3 , Pos = 2 
% 
% This second output can also be used to change the padding value into 
% something else than NaN. 
% 
%  [M, tf] = padcat(1:3,1,1:4) 
%  M(~tf) = 99 % change the padding value into 99 
% 
% Scalars will be concatenated into a single column vector. 
% 
% See also CAT, RESHAPE, STRVCAT, CHAR, HORZCAT, VERTCAT, ISEMPTY 
%   NONES, GROUP2CELL (Matlab File Exchange) 

% for Matlab 2008 and up (tested in R2015a) 
% version 2.2 (feb 2016) 
% (c) Jos van der Geest 
% email: [email protected] 

% History 
% 1.0 (feb 2009) created 
% 1.1 (feb 2011) improved comments 
% 1.2 (oct 2011) added help on changing the padding value into something 
%  else than NaN 
% 2.2 (feb 2016) updated contact info 

% Acknowledgements: 
% Inspired by padadd.m (feb 2000) Fex ID 209 by Dave Johnson 

narginchk(1,Inf) ; 

% check the inputs 
SZ = cellfun(@size,varargin,'UniformOutput',false) ; % sizes 
Ndim = cellfun(@ndims,varargin) ; % 

if ~all(Ndim==2) 
    error([mfilename ':WrongInputDimension'], ... 
     'Input should be vectors.') ; 
end 

TF = [] ; % default second output so we do not have to check all the time 

% for 2D matrices (including vectors) the size is a 1-by-2 vector 
SZ = cat(1,SZ{:}) ; 
maxSZ = max(SZ) ; % probable size of the longest vector 
% maxSZ equals : 
% - [1 1] for all scalars input 
% - [X 1] for column vectors 
% - [1 X] for all row vectors 
% - [X Y] otherwise (so padcat will not work!) 

if ~any(maxSZ == 1), % hmm, not all elements are 1-by-N or N-by-1 
    % 2 options ... 
    if any(maxSZ==0), 
     % 1) all inputs are empty 
     M = [] ; 
     return 
    else 
     % 2) wrong input 
     % Either not all vectors have the same orientation (row and column 
     % vectors are being mixed) or an input is a matrix. 
     error([mfilename ':WrongInputSize'], ... 
      'Inputs should be all row vectors or all column vectors.') ; 
    end 
end 

if nargin == 1, 
    % single input, nothing to concatenate .. 
    M = varargin{1} ; 
else 
    % Concatenate row vectors in a row, and column vectors in a column. 
    dim = (maxSZ(1)==1) + 1 ;  % Find out the dimension to work on 
    X = cat(dim, varargin{:}) ; % make one big list 

    % we will use linear indexing, which operates along columns. We apply a 
    % transpose at the end if the input were row vectors. 

    if maxSZ(dim) == 1, 
     % if all inputs are scalars, ... 
     M = X ; % copy the list 
    elseif all(SZ(:,dim)==SZ(1,dim)), 
     % all vectors have the same length 
     M = reshape(X,SZ(1,dim),[]) ;% copy the list and reshape 
    else 
     % We do have vectors of different lengths. 
     % Pre-allocate the final output array as a column oriented array. We 
     % make it one larger to accommodate the largest vector as well. 
     M = zeros([maxSZ(dim)+1 nargin]) ; 
     % where do the fillers begin in each column 
     M(sub2ind(size(M), SZ(:,dim).'+1, 1:nargin)) = 1 ; 
     % Fillers should be put in after that position as well, so applying 
     % cumsum on the columns 
     % Note that we remove the last row; the largest vector will fill an 
     % entire column. 
     M = cumsum(M(1:end-1,:),1) ; % remove last row 

     % If we need to return position of the non-fillers we will get them 
     % now. We cannot do it afterwards, since NaNs may be present in the 
     % inputs. 
     if nargout>1, 
      TF = ~M ; 
      % and make use of this logical array 
      M(~TF) = NaN ; % put the fillers in 
      M(TF) = X ; % put the values in 
     else 
      M(M==1) = NaN ; % put the fillers in 
      M(M==0) = X ; % put the values in 
     end 
    end 

    if dim == 2, 
     % the inputs were row vectors, so transpose 
     M = M.' ; 
     TF = TF.' ; % was initialized as empty if not requested 
    end 
end % nargin == 1 

if nargout > 1 && isempty(TF), 
    % in this case, the inputs were all empty, all scalars, or all had the 
    % same size. 
    TF = true(size(M)) ; 
end 
2

간단한 솔루션입니다 :

다음은 현재 버전입니다. 당신이 R2016b를 사용하는 경우 다음 splitrecommended 대신 strsplit

것을,

str = '1,2 3 4;1,2;1;'; 
arr = strsplit(str,';'); 
M = char(arr)-'0'; 
M(M<0) = 0; 
M(:,sum(M)==0)=[] 

참고 : 입력 새로운 라인을위한 하나의 공백 또는 쉼표 새로운 요소 및 ;로 구분, 문자의 목록입니다

그 결과

M =

1  2  3  4 
1  2  0  0 
1  0  0  0 
0  0  0  0 

그리고이 더 t와 숫자에 대한 버전 한 자릿수.

str = '1,12,300,4;1,23,3;1'; 
elem = str2num(char(strsplit(str,','))); 
arr = char(strsplit(str,';')); 
out = [ones(3,1) arr==',']; 
out(:,sum(out)==0) = []; 
out = out.'; 
out(out==1) = elem; 
out = out.' 

결과 :

out = 
    1 12 300  4 
    1 23  3  0 
    1  0  0  0 
+0

matlab에서'split'이 달력 함수로 나열됩니다. 그 MATLAB 동등한 알아? – kando

+0

@kando ['split'] (https://www.mathworks.com/help/matlab/ref/split.html)은 R2014b 이후 내장되어 있습니다. ['strsplit'] (https://www.mathworks.com/help/matlab/ref/strsplit.html)은 기능상의 그것과 유사합니다. –

+0

당신 말이 맞습니다. 내 2015b 설치에서'help split'은'help calendarDuration/split'을줍니다. 2016b 설치에서 문제가되지 않는 것 같습니다. – kando

관련 문제