From: ALittleDog on
I have two column 'a' and 'b', where 'a' stores the index, for
instance, a = [3; 1; 5; 4; 6] (the index nr., though integer, is not
continuous) and 'b' stores the data, for instance, b = [0.333123;
0.11233; 0.334112; 1.335353; 0.345323]. I want to extract the data
from b based on the index a. For example, I want to extract data from
b corresponding to an index a1 = [4; 5; 1] and save them in sequence
into column b1. (Of course, the real problem has a much larger
dimension.)
Could you please help me to solve this problem?
Thanks!
From: Wayne King on
ALittleDog <leqia.he(a)gmail.com> wrote in message <62a7094c-357f-44e1-a36c-a7ed0b027aed(a)s9g2000yqd.googlegroups.com>...
> I have two column 'a' and 'b', where 'a' stores the index, for
> instance, a = [3; 1; 5; 4; 6] (the index nr., though integer, is not
> continuous) and 'b' stores the data, for instance, b = [0.333123;
> 0.11233; 0.334112; 1.335353; 0.345323]. I want to extract the data
> from b based on the index a. For example, I want to extract data from
> b corresponding to an index a1 = [4; 5; 1] and save them in sequence
> into column b1. (Of course, the real problem has a much larger
> dimension.)
> Could you please help me to solve this problem?
> Thanks!

Hi, I'm not sure I understand exactly what you're trying to do, but what about

a = [3; 1; 5; 4; 6];
a1 = [4; 5; 1];
b = [0.333123; 0.11233; 0.334112; 1.335353; 0.345323];
[tf,loc] = ismember(a1,a);
b1 = b(loc);

Wayne
From: Walter Roberson on
ALittleDog wrote:
> I have two column 'a' and 'b', where 'a' stores the index, for
> instance, a = [3; 1; 5; 4; 6] (the index nr., though integer, is not
> continuous) and 'b' stores the data, for instance, b = [0.333123;
> 0.11233; 0.334112; 1.335353; 0.345323]. I want to extract the data
> from b based on the index a. For example, I want to extract data from
> b corresponding to an index a1 = [4; 5; 1] and save them in sequence
> into column b1. (Of course, the real problem has a much larger
> dimension.)
> Could you please help me to solve this problem?

That appears to be simply
b1 = b(a1);
unless I am missing something?

If a1 has to be matched against the contents of "a" and the corresponding
member of b is looked up,

[tf, idx] = ismember(a1, a);
b1 = b(idx);

From: ALittleDog on
Dear Walter and Wayne,

thank you for posting your codes. They really help!

Leqia