How to put for loop outcome into 1 column in right order

Hi guys, I made the following for loop where I calculated the response errors for each target. Now instead of putting them in a new matrix in 4 different columns, I want to put the error responses (err) as 1 column in the old matrix (res), in the correct order (so each error response matches its trials). Can somebody explain how to do this?
res = S.res;
trials = res(:,1);
targets = res(:,7);
responses = res(:,8);
oria = [3,4,5,6];
tar = [1,2,3,4];
num = numel(oria);
out = cell(1,num);
for k = 1:num
tarsel = targets==tar(k);
resp = responses(tarsel);
orib = res(tarsel,oria(k));
err = resp - orib;
err(err<-90) = err(err<-90)+180;
err(err>90) = err(err>90)-180;
out{k} = err;
end
out = [out{:}];

Answers (1)

Can't you just say
res(:, 1) = err(:);
inside the loop? But every iteration will overwrite column 1 of course so not sure why, or if, you want that.
If that doesn't work, please attach your variables in a .mat file.

7 Comments

I tried it, but it gives the following message: Unable to perform assignment because the size of the left side is 10000-by-1 and the size of the right side is 2500-by-1. But the outcomes are each 2500 values, so it should be 10000 in total.
Did you overlook my last sentence? Here it is again:
"If that doesn't work, please attach your variables in a .mat file."
save('answers.mat', 'S');
Just look at the sizes of each variable in your loop and try to understand what's going on. YOu're creating variables of lots of different sizes so err is not the same number of rows as res.
But the variables each 4 have 2500 values, and res has 10000 values. So shouldn't it be possible to add a column to res?
You can overwrite 2500 values of a longer vector with 2500 values of a shorter vector. Is that what you want to do? If so, why? And what do you want to do with the other 7500 values? Just leave them there as-is? Or erase them to zeros?
No I do not want to overwrite them. I want each response error value to be in the row with its corresponding trial (in column res(:,9)). Because I had to calculate the errors based on 4 different targets, there are 4 different groups and I can't get the response errors to match the correct trial.

Sign in to comment.

Categories

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

Tags

Asked:

on 26 Oct 2020

Commented:

on 26 Oct 2020

Community Treasure Hunt

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

Start Hunting!