how to store a double loop result into a matrix column by column?
1 view (last 30 days)
Show older comments
I have a matrix cycle; What I am doing is to find out certain points satisfying the condition and store them into Array up and down;
When cycle1 is one column, it could work; but when cycle1 increases to 2 columns, then the double loop doesn't work.
up and down's size is not certain; I just estimate their size 100*12;
now the code has no error, but give wrong results.
please give me some suggestion about this code.
cycle1 = [butterworth_filter1,butterworth_filter2];
[m,n] = size(cycle1)
up = zeros(100,12);
down = zeros(100,12);
k=1;
for j = 1:n
for i = 1:m
if i == m
break
elseif cycle1(i,j) < 1.5e-5 && cycle1(i+1,j) > 1.5e-5
up(k,:) = i + 1;
k = k+1;
elseif cycle1(i,j) > 1.5e-5 && cycle1(i+1,j) < 1.5e-5
down(k,:) = i;
k = k+1;
end
end
end
6 Comments
Geoff Hayes
on 29 Sep 2021
So if (i,j) is "too big" then is k good enough? If not, where is the error?
Stephen23
on 29 Sep 2021
LEI Li's incorrectly posted "Answer" moved here:
from the test code, up has the result; now each column is the same; I need to chech the random matrix;
next thing is there should not exist 0 in the column; this is the reason I use k;
previously when I deal with one dimension array, I use
up(end+1) = i + 1; to store the location; but now I need to deal with a matrix; I stuck here.
Answers (1)
Jan
on 29 Sep 2021
You are using one counter k for the up and the down lists.
cycle1 = [butterworth_filter1,butterworth_filter2];
[m, n] = size(cycle1)
up = zeros(100, 12);
down = zeros(100, 12);
kup = 1;
kdown = 1;
for j = 1:n
for i = 1:m - 1 % Simpler than: if i==m, break
if cycle1(i,j) < 1.5e-5 && cycle1(i+1,j) > 1.5e-5
up(kup,:) = i + 1; % Are you sure you want to set the complete row to i+1?
kup = kup + 1;
elseif cycle1(i,j) > 1.5e-5 && cycle1(i+1,j) < 1.5e-5
down(kdown, :) = i;
kdown = kdown + 1;
end
end
end
See Also
Categories
Find more on Annotations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!