Delete last n1,n2,n3 (...) elements in each row in a Matrix in an efficient way or set to zero
1 回表示 (過去 30 日間)
古いコメントを表示
I've a large set of data and i want to remove from the end of each row a different amount of elements (0-n). Is there a fast way to do this without using a loop?
As an example: A= % In reality different numbers, not only ones!
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
Then I've a vector saying how many elements to delete from each row, e.g.:
B =
1 3 0 2 1
So the result would look like:
A2
1 1 1 1 0
1 1 0 0 0
1 1 1 1 1
1 1 1 0 0
1 1 1 1 0
Thanks so much in advance!
0 件のコメント
採用された回答
Roger Stafford
2014 年 2 月 22 日
[m,n] = size(A);
p = cumsum(accumarray([(1:m)',n+1-B'],1,[m,n+1]),2);
A(p(:,1:n)==1) = 0;
0 件のコメント
その他の回答 (1 件)
Image Analyst
2014 年 2 月 21 日
As long as you want to simply set to zero, no need to use cell arrays:
A=[...
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1]
B =[1 3 0 2 1];
for row = 1 : size(A, 2)
A(row,(end-B(row)+1):end) = 0;
end
A % Print to command window.
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Matrices and Arrays についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!