MATLAB: Question about how to make something that points to a matrix

casepoint

I'm pretty new to matlab, so sorry if my question seems a bit dumb to ask or vague.
Example:
x=[11, 3, 5, 7, 1, 8, 9];
y=[1, 2];
I have a program that gives me a matrix x and I made another program that outputs matrix y. What matrix y is doing is searching for a specific value in some other matrix and its telling me what columns that specific value is in, and it gets saved into matrix y. In this example the specific value I'm looking for is in column 1, and 2.
So what I now want to do with these 2 matrices is make matrix y point to matrix x, and return the values in x to y.
Meaning matrix y tells me that the specific value I want is in columns 1 and 2, now I want to tell matrix y to look at matrix x, and see that for the 1st column we have 11, and for column 2 we have 3, now replace the y=[1, 2] with y=[11, 3].
So for another example we could have had y=[1, 4], and the program would know that it correlates with the values 11, and 7, and replace matrix y with a new matrix y=[11, 7].
Basically I want the program to know that each value it has in matrix y relates to a specific column in the 1×7 matrix x.
I tried doing some case structure, but it failed to run properly.

Best Answer

Dear Gilmar, here is the code:
x = [11, 3, 5, 7, 1, 8, 9];
y = [1, 2];
if max(y) > length(x)
error('Maximum column index pointer in y is greater than length of vector x')
else
y(1:end) = x(y(1:end));
end
disp(y)
Good luck!