1

I have data on several subjects that each performed several trials of an activity. I have read all the data into structs in the format subject(1).trial(1).something subject(1).trial(2).somethingelse etc.

Now I need to read each of the trials into a row a big matrix [A] to perform some calculations on each trial, as if the subject didn't matter. So I started with this:

for i = 2:numSubjects
    for j = 1:numTrials
    A(j,:) = cat(2,subject(i).trial(j).torque_integral,     subject(i).trial(j).work_integral); 
    end
end

But this will only work for the first subject. When the subject (i) increments to 3, the trial (j) will be back at one. So the idea is the output A lines up like this:

subject|trial|A

1 1 1

1 2 2

1 3 3

2 1 4

2 2 5

2 3 6

Hopefully this is clear. Any thoughts?

1
  • Looks like the loop is iterating through each subject (ignoring the first), and then each trial for each subject. The code generally matches the output which matches what I might want to do with this loop. What do you want to do? Can you post an table of what you want to see (subject, trial)? Commented Mar 11, 2013 at 23:24

2 Answers 2

0

If i read your question correctly, you need to put each trial in one row, so when make your A variable, instead of using j as index, just use a new index... lol seems like someone just posted the same thing before me...

m=1
for i = 2:numSubjects
    for j = 1:numTrials
       A(m,:) = cat(2,subject(i).trial(j).torque_integral,subject(i).trial(j).work_integral);
       m=m+1; 
    end
end
Sign up to request clarification or add additional context in comments.

Comments

0

I am not quite sure I understand everything you do or want to do, but obviously if you have two for loops, of which the inner one starts with 1 it will start with one for each increment of the outer...

How about:

k=0;
for i = 2:numSubjects
    for j = 1:numTrials
    k=k+1;
    A(k,:) = cat(2,subject(i).trial(j).torque_integral,     subject(i).trial(j).work_integral); 
    end
end

Hope that helps.

1 Comment

yes thanks! i knew it was easy just couldn't wrap my head around it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.