how to store a double loop result into a matrix column by column?

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

Lei - you say that the code has no error, but gives wrong results. What is wrong about the results? What are you expecting? Should you be using k for both the up and down arrays? Why are these arrays 100x12 - do you really need 12 columns or would one suffice?
I use the code to test it; no error; but it writes the result to the array; but I didn't know how to eliminate the redundent zero. I need 12 columns bc I have 12 data array;
I try to find certain value in cycle1 and record their locations; then I will use these locations to find the data set between up and down.
close all;
clear;
clc;
cycle1 = randn(100,12);
[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) < 0.2 && cycle1(i+1,j) > 0.2
up(k,:) = i + 1;
k = k+1;
elseif cycle1(i,j) > 0.2 && cycle1(i+1,j) < 0.2
down(k,:) = i;
k = k+1;
end
end
end
Lei - where are the 12 arrays in your code? Or do you have variables called cycle2, cycle3, etc.? From your comment I try to find certain value in cycle1 and record their locations;... since the location is a pair (i,j) do you need to store these two values? Why k?
this is the original code.
the reason I use k is to push the selected location into cycle1. the index (i,j) will be too big for selected location.
close all;
clear all;
clc;
emg = fopen('C:\MGasR.csv');
%% Read Data Columns;
res = textscan(emg, '%f %f %f %f %f %f %f %f %f %f %f %f','headerlines',2);
fclose(emg);
%% Data Matrix
T = cell2table(res);
A = cell2mat(res);
%% Raw Signal
figure(1)
plot(A(:,1),A(:,8), A(:,1), A(:,9), A(:,1),A(:,10));
grid on
xlabel('Time(s)');ylabel('EMG (V)');title('Raw EMG Signal')
legend ('Subject 2')
%% Rectified Signal
rec_emg=abs(A);
figure(2)
plot(rec_emg(:,1),rec_emg(:,8), rec_emg(:,1),rec_emg(:,7));
grid on
xlabel('Time(s)');ylabel('EMG (V)');title('Rectified EMG Signal [Full-Wave Rectification]')
legend ('Subject 2')
%% Filtered Signal
[a,b]= butter(4, 0.002);
butterworth_filter1 = filter(a, b, rec_emg(:,8));
butterworth_filter2 = filter(a, b, rec_emg(:,7));
butterworth_filter3 = filter(a, b, rec_emg(:,4));
butterworth_filter4 = filter(a, b, rec_emg(:,5));
butterworth_filter5 = filter(a, b, rec_emg(:,6));
figure (3)
%Plotting style(res{1,1}, res{1,2}, res{1,1}, res{1,3})
plot(rec_emg(:,1),butterworth_filter1, rec_emg(:,1),butterworth_filter2);
xlabel('Time(s)');ylabel('EMG(V)');title('Filtered EMG Signal [Butterworth]')
legend ('Subject 2')
cycle1 = butterworth_filter1;
up = [];
down = [];
for i = 1:length(cycle1)
if i == length(cycle1)
break
elseif cycle1(i) < 1.5e-5 && cycle1(i+1) > 1.5e-5
up(end+1) = i + 1;
elseif cycle1(i) > 1.5e-5 && cycle1(i+1) < 1.5e-5
down(end+1) = i;
end
end
figure (4)
plot(cycle1)
hold on
plot(up, cycle1(up), 'go', 'MarkerSize', 8, 'MarkerFaceColor', 'g')
plot(down, cycle1(down), 'ro', 'MarkerSize', 8, 'MarkerFaceColor', 'r')
xlabel('Time(s)');ylabel('EMG(V)');title('Muscle Activation & Cycles')
legend ('Subject 1')
So if (i,j) is "too big" then is k good enough? If not, where is the error?
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.

Sign in to comment.

Answers (1)

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

1 Comment

close all;
clear;
clc;
cycle1 = abs(randn(100,12));
for i = 1:12
plot(cycle1(:,i))
hold on
end
[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
% if i == m-1
% break
if cycle1(i,j) < 0.2 && cycle1(i+1,j) > 0.2
up(kup,:) = i + 1; %up only has one value now??
kup = kup+1;
elseif cycle1(i,j) > 0.2 && cycle1(i+1,j) < 0.2
down(kdown,:) = i;
kdown = kdown+1; %down only has one value now??
end
end
end
figure (4)
plot(cycle1)
hold on
plot(kup, cycle1(kup), 'go', 'MarkerSize', 8, 'MarkerFaceColor', 'g')
plot(kdown, cycle1(kdown), 'ro', 'MarkerSize', 8, 'MarkerFaceColor', 'r')
xlabel('Time(s)');ylabel('EMG(V)');title('Muscle Activation & Cycles')
legend ('Subject 1')

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Asked:

on 29 Sep 2021

Commented:

on 29 Sep 2021

Community Treasure Hunt

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

Start Hunting!