I am new to matlab.
I am trying to use matlab for a psychology experiment. I obtained data in a matrix with two columns. The first column refers to the distance in cm and the second column to reaction time. The data matrix that I have looks something like this:
distance = [ 0.1 0.332;
0.2 0.678;
0.1 0.876;
0.3 0.765;
0 0.345;
0.2 0.541;
0.3 0.541;
0.1 0.567;
0.2 0.123;]
The distance (column 1) can be 0, 0.1, 0.2 or 0.3. I would like separate matrices for each of these distances (so I can compare their reaction times).
I wrote a for loop
S = 1;
T = 1;
U = 1;
V = 1;
for F = 1:length(distance)
if distance(F,1) == 0
distance_0(S,1) = distance(F,2);
S = S + 1;
elseif distance(F,1) == 0.1
distance_1(T) = distance(F,2);
T = T + 1;
elseif distance(F,1) == 0.2
distance_2(U) = distance(F,2);
U = U + 1;
else distance(F,1) == 0.3
distance_3(V) = distance(F,2);
V = V + 1;
end
end
The output should look like this:
distance_0 = [0.345]
distance_1 = [0.332; 0.876; 0.567]
distance_2 = [0.678; 0.541; 0.123]
distance_3 = [0.765; 0.541]
However, it is not working well and I don' t get the above output matrices. The matrices distance_* are only filled partly. What am I doing wrong! I hope someone can help me. Thanks in advance.

 Akzeptierte Antwort

Azzi Abdelmalek
Azzi Abdelmalek am 16 Dez. 2013
Bearbeitet: Azzi Abdelmalek am 16 Dez. 2013

0 Stimmen

clear
distance = [ 0.1 0.332;
0.2 0.678;
0.1 0.876;
0.3 0.765;
0 0.345;
0.2 0.541;
0.3 0.541;
0.1 0.567;
0.2 0.123;];
%---------- ------------------------------------------------
n=size(distance,1);
S = 1;
T = 1;
U = 1;
V = 1;
for F = 1:n
if distance(F,1) == 0
distance_0(S,1) = distance(F,2);
S = S + 1;
elseif distance(F,1) == 0.1
distance_1(T) = distance(F,2);
T = T + 1;
elseif distance(F,1) == 0.2
distance_2(U) = distance(F,2);
U = U + 1;
elseif distance(F,1) == 0.3
distance_3(V) = distance(F,2);
V = V + 1;
end
end
disp(distance_0)
disp(distance_1)
disp(distance_2)
disp(distance_3)

3 Kommentare

Maya
Maya am 16 Dez. 2013
Thanks for your quick response. I think my question was not very clear. I edited my question to be more clear. It would be great if you could have a look again!
Azzi Abdelmalek
Azzi Abdelmalek am 16 Dez. 2013
Look at edited answer
Maya
Maya am 18 Dez. 2013
great, thanks!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Psychology finden Sie in Hilfe-Center und File Exchange

Tags

Gefragt:

am 16 Dez. 2013

Kommentiert:

am 18 Dez. 2013

Community Treasure Hunt

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

Start Hunting!

Translated by