# How to create a variable that cataloges in two groups the values that another variable takes in MATLAB

3

I have a variable "variant" that takes the following values: 1, 2, 3, 4, 5 and 6.

What I need to do, is a new variable called `amistad` that takes value 1 for the cases in which the `variante` takes value 1, 3, 5 and that takes value 2 for the cases in which `variante` takes value 2, 4 and 6.

Try the following loop:

``````amistad= amistad(296,1)
for i=1:296
i= variante
if i== 1||3||5
if i == 2||4||6
end
``````

But at no time I create the variable `amistad` and I do not know if this is the right way to do it.

asked by Tamara N 11.06.2016 в 22:19
source

1

Subject to some questions being omitted, I would like to mention the following points:

I understand that the variable `amistad` is a vector of 296x1 elements, then, it is incorrect to initialize the variable `amistad` in that way, given that you have not previously defined it (of course, unless you have a class definition) or function that returns a matrix of those dimensions, which I do not know now). The appropriate thing would be to use the `zeros` function to initialize a vector of zeros that you will fill in as necessary, that is:

``````amistad = zeros(296,1);
``````

Now, there is something that seems to me somewhat "unusual" in your code: why do you "manually" modify the value of the variable i ?. Normally a cycle variable does not modify it, you can use it to operate with it, but not modify it. In this case, the value of your cycle variable will always be the same: the one assigned to variant , and therefore you will always be "filling in" the same position, which is absurd since you do not need a loop for this.

Another point you should review is regarding your condition `if` , would not it be better to check if the number in question is even or odd, instead of explicitly placing the numbers?.