How do I index individual columns in an array?

Hi,
I have a fairly basic question that I cant seem to solve.
I have an array "high_perspective_taking" which has values from 0-4. I would like to write a for loop that goes through all the rows of columns 1 and 4 in order to change all the 4's to 0's, 3's to 1's, 1's to 3's, and 0's to 4's.
I have the following for loop but I am struggling on how to index both column 1 and column 4.
for p = 1:length(high_perspective_taking)
if high_perspective_taking(p,[1 4])== 0;
high_perspective_taking(p,[1 4])= 4;
end
if high_perspective_taking(p,[1 4])== 1;
high_perspective_taking(p,[1 4])= 3;
end
if high_perspective_taking(p,[1 4])== 3;
high_perspective_taking(p,[1 4])= 1;
end
if high_perspective_taking(p,[1 4])== 4;
high_perspective_taking(p,[1 4])= 0;
end
end
Thank you!

 Réponse acceptée

load high_perspective_taking.mat
% original:
high_perspective_taking
high_perspective_taking = 19×7
2 3 2 1 0 1 3 1 2 3 3 2 3 3 3 0 1 4 2 0 4 3 2 3 4 1 1 3 0 4 3 1 0 4 3 1 3 2 1 2 1 2 2 2 4 0 2 2 0 1 1 1 3 2 1 2 4 3 3 3 1 1 2 3 2 1 2 2 1 1
% 0 <-> 4, 1 <-> 3:
high_perspective_taking(:,[1 4]) = 4 - high_perspective_taking(:,[1 4])
high_perspective_taking = 19×7
2 3 2 3 0 1 3 3 2 3 1 2 3 3 1 0 1 0 2 0 4 1 2 3 0 1 1 3 4 4 3 3 0 4 3 3 3 2 3 2 1 2 2 2 4 4 2 2 0 3 1 1 1 2 1 2 0 3 3 1 1 1 2 1 2 1 2 2 1 1

6 commentaires

There are a couple of problems with your approach.
One is that this condition, for instance
if high_perspective_taking(p,[1 4])== 0;
will be considered true only if both high_perspective_taking(p,1) and high_perspective_taking(p,4) are 0. But based on your description it sounds like you want to check each element individually.
The second problem is that doing the replacements in sequence won't work, in general. For instance, if step one is to replace the zeros with fours, then a later step is to replace the fours with zeros, you end up replacing the fours that you put in place in step one. A concrete example to illustrate the problem (just doing the swapping of zeros and fours, to simplify):
A = [0:4; 0:4]
A = 2×5
0 1 2 3 4 0 1 2 3 4
A(A == 0) = 4
A = 2×5
4 1 2 3 4 4 1 2 3 4
A(A == 4) = 0
A = 2×5
0 1 2 3 0 0 1 2 3 0
Instead, you should first store the information about which value is where, for all values you want to replace, then replace them all based on that information. For instance:
A = [0:4; 0:4]
A = 2×5
0 1 2 3 4 0 1 2 3 4
A_is_zero = A == 0;
A_is_four = A == 4;
A(A_is_zero) = 4;
A(A_is_four) = 0;
A
A = 2×5
4 1 2 3 0 4 1 2 3 0
lil brain
lil brain le 22 Juil 2022
Ah, ok I understand now. Thank you this is very helpful I wouldn't have noticed this logical error.
Works like a charm!
Voss
Voss le 22 Juil 2022
Great! You're welcome!
@Voss one thing I am still wondering is how I can apply your approach when only needing to change individual columns. I have tried indexing the the original array and then changing the values the logical array only contains the number of columns that I have specified.
Like when I use this I get a wrong result:
A_is_zero = A(:,[1 4]) == 0;
A_is_four = A(:,[1 4]) == 4;
A(A_is_zero) = 4;
A(A_is_four) = 0;
How can I use your approach with specifically indexed columns?
lil brain
lil brain le 23 Juil 2022
I am just curious because I used the approach in your answer but I am wondering if it also works using logicals?
Well, if you're stil wanting to swap 0s with 4s and 1s with 3s, I would still use the approach in my answer, i.e.:
high_perspective_taking(:,[1 4]) = 4 - high_perspective_taking(:,[1 4])
In my comment I wanted to show a more general approach to swapping values where the relation was not necessarily as simple as x = 4-x, but you're right I neglected to show how to apply that approach only to certain columns.
Probably the easiest way would be to create a temporary variable containing just the relevant columns, then do the swapping in that variable, then assign back to the original variable. For example:
% a matrix where 0s and 4s in columns 1 and 4 will be swapped:
A = [0 0 0 0; 0 1 2 3; 1 2 3 4; 4 4 4 4]
A = 4×4
0 0 0 0 0 1 2 3 1 2 3 4 4 4 4 4
% temporary variable containing just columns 1 and 4 of A:
A14 = A(:,[1 4]);
% swap 0s and 4s in A14:
A14_is_zero = A14 == 0;
A14_is_four = A14 == 4;
A14(A14_is_zero) = 4;
A14(A14_is_four) = 0;
% assign back to A:
A(:,[1 4]) = A14
A = 4×4
4 0 0 4 4 1 2 3 1 2 3 0 0 4 4 0

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by