Extraction of a sub matrix from a mother matrix
2 ビュー (過去 30 日間)
古いコメントを表示
Hi, I have a matrix A = [0 0 1 1 0 0 0 1 1 1 1 1 0 0 0 0 1 1 1 0 0 1]. I want to sum the 1's of its duration so that the result comes as B = [0 0 2 0 0 0 5 0 0 0 0 3 0 0 1]. I need a help in this regard.
0 件のコメント
採用された回答
Jan
2018 年 1 月 10 日
編集済み: Jan
2018 年 1 月 10 日
A = [0 0 1 1 0 0 0 1 1 1 1 1 0 0 0 0 1 1 1 0 0 1];
[B, N] = RunLength(A);
match = (B == 1);
B(match) = N(match);
N(match) = 1;
Result = RunLength(B, N);
[EDITED] And after a lot of time and struggling a working loop:
R = zeros(1, numel(A) + 1);
iR = 1;
for k = 1:numel(A)
if A(k) == 0
iR = iR + 1 + (R(iR) ~= 0);
else
R(iR) = R(iR) + 1;
end
end
R = R(1:iR - (R(iR) == 0))
6 件のコメント
Jan
2018 年 1 月 11 日
@Amitrajit Mukherjee: You can download the files on the computer you use to post in the forum and include the source codes in your program. Copying the code from the forum is not a real difference to copying code from the FileExchange.
その他の回答 (2 件)
Birdman
2018 年 1 月 10 日
Firstly, download the file from the following link and use rcumsum function within it.
Then, apply the following code:
B=rcumsum(A);
idx=setdiff(find(diff(B)==1),find(diff(B)<0)-1)+1;idx(end)=[];
B(idx)=[]
7 件のコメント
Stephen23
2018 年 1 月 10 日
>> A = [0 0 1 1 0 0 0 1 1 1 1 1 0 0 0 0 1 1 1 0 0 1];
>> B = rcumsum(A);
>> B = B(B==0 | [diff(B)<0,true])
B =
0 0 2 0 0 0 5 0 0 0 0 3 0 0 1
2 件のコメント
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!