필터 지우기
필터 지우기

How to combine vectors of different length into matrix ?

조회 수: 1 (최근 30일)
Neelabh
Neelabh 2012년 6월 18일
Actually..what I want to do is...
I have a vector A =[ 1 2 3 4 5] and a matrix B = [3 4 5 0 0; 1 2 4 5 0; 4 5 0 0 0]
I want save setdiff(A,B(i,:)) i.e. difference of each row of B from A
[1 2] , [3], [1 2 3] into a matrix in the form of [1 2 0; 3 0 0; 1 2 3] (padding with '0')
Please help....
Thanks...

채택된 답변

Sean de Wolski
Sean de Wolski 2012년 6월 18일
I would just use a for-loop and some fun indexing:
A =[ 1 2 3 4 5];
B = [3 4 5 0 0; 1 2 4 5 0; 4 5 0 0 0];
sz = size(B);
C = zeros(sz);
nkeep = -inf; %how many columns to keep?
for ii = 1:sz(1)
temp = setdiff(A,B(ii,:));
ncurr = numel(temp);
nkeep = max(nkeep,ncurr);
C(ii,1:ncurr) = temp;
end
C = C(:,1:nkeep);

추가 답변 (1개)

Thomas
Thomas 2012년 6월 18일
Just for fun.. Another complicated way..
A =[ 1 2 3 4 5];
B = [3 4 5 0 0; 1 2 4 5 0; 4 5 0 0 0];
for ii=1:size(B,1)
c(ii,:)=[setdiff(A,B(ii,:)),zeros(1,length(B)-length(setdiff(A,B(ii,:))))];
end
c

카테고리

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