how to convert 2d to id
1 次查看(过去 30 天)
显示 更早的评论
i want to convert a 5x3 matrix say A into 1d but before reshaping it i need to omit certain values from every row of A.number of values i need to omit is given in another matrix say C. for eg
A=[5 4 3 8 9 6; 2 1 3 2 5 ;4 3 5 7 6]
C=[1; 3; 4]
what i need is to convert matrix A into an array such that from first row of A last one value is to be emitted from second row last 3 values are to be removed and similarly from third row last 4 values need to be omitted. number of values to be omitted is given in row matrix C
please help
2 个评论
Stephen23
2016-7-6
@studentambitious: your matrix A generates this error:
>> A=[5 4 3 8 9 6; 2 1 3 2 5 ;4 3 5 7 6]
Error using vertcat
Dimensions of matrices being concatenated are not consistent.
回答(3 个)
KSSV
2016-7-6
A=[5 4 3 8 9 ; 2 1 3 2 5 ;4 3 5 7 6] ;
B = [1 3 4] ;
iwant = fliplr(A) ;
for i = 1:length(B)
id = B(i) ;
iwant(i,1:id) = NaN ;
end
iwant = fliplr(iwant) ;
idx = ~isnan(iwant) ;
iwant = iwant(idx) ;
0 个评论
Azzi Abdelmalek
2016-7-6
编辑:Azzi Abdelmalek
2016-7-6
A=[5 4 3 8 9 ; 2 1 3 2 5 ;4 3 5 7 6]
C=[1; 3; 4]
n=size(A,2);
m=numel(C);
out=A(:);
idx=arrayfun(@(x) sub2ind(size(A),x*ones(1,C(x)),n:-1:n-C(x)+1),(1:m),'un',0)
idx=cell2mat(idx)
out(idx)=[]
%Or
A=[5 4 3 8 9 ; 2 1 3 2 5 ;4 3 5 7 6];
C=[1; 3; 4];
[n,m]=size(A);
B=zeros(1,n*m-sum(C));
idx=1;
for k=1:n
kk=1:m-C(k);
p=numel(kk);
B(1,idx:idx+p-1)=A(k,kk);
idx=idx+p;
end
B
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!