Result according to corresponding variables

Hi!
I'd like to obtain the result in the output according to the variables A1, A2, A3 and values.
A1 = [100; 130; 160; 2100]
A2 = [200; 10; 150; 230]
A3 = [300; 120; 1400; 220]
Values = [100; 1400; 200; 150; 1400]
output = [A1; A3; A2; A2; A3]
Please, how can I achieve this? May someone help me?

2 Comments

How are the input and output related?
While I might make a wild guess that a sort is somehow important here, there are THREE vectors A1,A2,A3, but there are 4 diatinct values in the vector Values. So how should we guess to do what here?
The crystal ball is just so foggy today.
Thanks for you reply.
I'll try to explain.
The vector A1, A2 and A3 are 4 fix values.
The vector " Values" show numbers which are found in the vectors A1, A2 and A3.
I'd like to find which vector A1, A2 or A3 each number in the Value vector repeats?
For example:
Values = [ 130; 1400; 230]
The answer will be:
output = [ A1; A3; A2]

Sign in to comment.

 Accepted Answer

We're going down a bad road if we're trying to use variable names as variables. If instead of using numbered variables, A1,A2,A3, etc were column vectors of an array:
A1 = [100; 130; 160; 2100];
A2 = [200; 10; 150; 230];
A3 = [300; 120; 1400; 220];
A = [A1 A2 A3];
Values = [100; 1400; 200; 150; 1400; 4]; % what if numbers aren't in A?
[~,idx] = ismember(Values,A);
idx(idx==0) = NaN; % ignore values which are not members of A
[~,col] = ind2sub(size(A),idx)
col = 6×1
1 3 2 2 3 NaN
The result is the column subscript describing where the number is. This makes the output usable, as A can simply be indexed by subsequent code. A vector of variable names is not useful for anything but creating more problems.
If A1, etc are not the same length, similar can be done with a cell array and some modification.

1 Comment

Many Thanks!! It works for my problem. Thanks for your explanation!

Sign in to comment.

More Answers (0)

Categories

Tags

Asked:

on 30 Sep 2021

Commented:

on 30 Sep 2021

Community Treasure Hunt

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

Start Hunting!