Hi everyone,
I have an array such as;
input = [1 -2 -1 -1 -1 0 0 -1 0 0 0 3 0 0 4 0 0 0 0 0]
By counting zeros and determining the value after zero, I want to create a new dimentional array such as;
output = [0 1; 0 -2; 0 -1; 0 -1; 0 -1; 2 -1; 3 3; 2 4; 4 0]
Each row of "output" array determines that [number of zeros before non zero element non zero element].
For example, [2 4] represents that there are 2 zeros before "4".
How can I create the "output" array based on this rule?

1 件のコメント

Image Analyst
Image Analyst 2019 年 5 月 6 日
Just try a for loop.

サインインしてコメントする。

 採用された回答

Adam Danz
Adam Danz 2019 年 5 月 6 日
編集済み: Adam Danz 2019 年 5 月 7 日

1 投票

Loop method
input = [1 -2 -1 -1 -1 0 0 -1 0 0 0 3 0 0 4 0 0 0 0 0];
output = nan(numel(input),2);
for i = 1:numel(input)
if input(i)==0
continue
end
output(i,:) = [max(cumsum(input(1:i)==0)),input(i)];
input(1:i) = 1; %make sure all previous 0s are overwritten
end
% if input ended in 0, count the consecutive 0s minus 1 (which matches the example)
if input(end)==0
output(end,:) = [sum(input==0)-1,0];
end
% get rid of leftover output rows
output(isnan(output(:,1)),:) = [];
Without a loop
inputTemp = [1,input(1:end-1),1]; %make sure last digit is non-zero (for now)
cs = cumsum(inputTemp==0); %cumulative sum of 0-counts
zeroCounts = diff(cs(inputTemp~=0)); %count consecutive zeros
nonZeros = [input(input~=0 & 1:numel(input)<numel(input)),input(end)];
output = [zeroCounts', nonZeros'];
Result for both methods
output =
0 1
0 -2
0 -1
0 -1
0 -1
2 -1
3 3
2 4
4 0

5 件のコメント

cem
cem 2019 年 5 月 7 日
I really thank you, it works perfectly. Also I wonder is there any solution way without using loop?
Adam Danz
Adam Danz 2019 年 5 月 7 日
I updated my answer with a non-loop method. What makes this tricky is that the last number in your example is a zero but it's counted as if it were not a zero. That's built in to both solutions but makes the method a little more klunky, but functional.
cem
cem 2019 年 5 月 7 日
Thank you for both solutions, Elapsed time of first solution takes 0.009498 seconds, and the other version without loop code takes 0.007459 seconds at the same computer.
Adam Danz
Adam Danz 2019 年 5 月 7 日
編集済み: Adam Danz 2019 年 5 月 7 日
What are you going to do with all of that extra time? :D
Readability is always important, too. Especially if other people will work with your code some day. That being said, I'm not sure which of my proposals is more readable.
cem
cem 2019 年 5 月 7 日
I will take a Couple of coffee at extra time;-) In my opinion loop solution nötr readable than second one. Thank you again.

サインインしてコメントする。

その他の回答 (1 件)

Stephen23
Stephen23 2019 年 5 月 7 日
編集済み: Stephen23 2019 年 5 月 7 日

3 投票

Simpler:
>> V = [1,-2,-1,-1,-1,0,0,-1,0,0,0,3,0,0,4,0,0,0,0,0];
>> X = find([V(1:end-1),1]);
>> Z = [diff([1,X+1])-1;V(X)].'
Z =
0 1
0 -2
0 -1
0 -1
0 -1
2 -1
3 3
2 4
4 0

3 件のコメント

Adam Danz
Adam Danz 2019 年 5 月 7 日
Nice. I was certain there was a simpler solution but gave in to time!
Stephen23
Stephen23 2019 年 5 月 7 日
@Adam Danz: thank you. Together diff and find are great for these kind of things, but there appears to be no shortcut: I just sit and puzzle them out the hard way...
cem
cem 2019 年 5 月 7 日
Waow that was super! Thank you Stephen!

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

質問済み:

cem
2019 年 5 月 6 日

コメント済み:

cem
2019 年 5 月 7 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by