How may I use reshape for this code?

1 view (last 30 days)
Onur Batin Genc
Onur Batin Genc on 31 Jul 2022
Answered: Jan on 31 Jul 2022
count=0;
for j = 1:frame_number
for i=1:15
if (A(i,j)*A(i+1,j))<0
count=count+1;
end
b(j)=count;
end
count=0;
end
  3 Comments
Onur Batin Genc
Onur Batin Genc on 31 Jul 2022
Thats the main code of my approach. I was trying to use reshape instead of for loop to get zero crossing points.

Sign in to comment.

Accepted Answer

Jan
Jan on 31 Jul 2022
Your loop method overwrites b(j) repeatedly. Setting count to 0 in two lines is confusing. Simpler:
for j = 1:frame_number
count=0;
for i = 1:15
if (A(i,j) * A(i+1,j)) < 0
count = count + 1;
end
end
b(j) = count;
end
In a next step of simplification you can omit the if branch and add the result of the condition directly:
for j = 1:frame_number
count=0;
for i = 1:15
count = count + ((A(i,j) * A(i+1,j)) < 0);
end
b(j) = count;
end
If the condition is true, its value is converted to 1. false is converted to 0.
But you can omit the loop completely:
AA = A(1:15, :) .* A(2:16, :);
b = sum(AA < 0, 1);
There is no need for a reshape command.

More Answers (0)

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!