From: guyz Smarty on
Hi all,

I am new to the MATLAB programming and am trying to learn now. I am stuck with a problem ( seems bigger for me, but may not be the same for you ;) ).

Suppose A = [2 3 4] .... A vector

and B = [2 1 2
3 4 2
3 5 4] ..... A Matrix

now I want to check which elements of B are equal to each element of A individually, i.e., I want matrices like below:

find(B==A(1))
1 0 1
0 0 1
0 0 0

and

find(B==A(2))
0 0 0
1 0 0
1 0 0

and

find(B==A(3))
0 0 0
0 1 0
0 0 1

But I don't want to do it, three times manually. Also I don't want to do it with FOR loops (someone told me if I use FOR loops in MATLAB, then I am abusing it)

Please give me some directions to follow.

Any help will be appreciated.

Thanks
From: Nic Roberts on
Hi

I have absolutly no problem using for loops they are really useful for a problem like this.

A=[2 4 3]
B=[2 1 2; 3 4 2; 3 5 4]
R=[0 0 0; 0 0 0; 0 0 0]
for i=1:3
for j=1:3
for k=1:3
if A(i)=B(j,k)
R(j,k)=1
end
end
end

Hope that helps,

Nic

"guyz Smarty" <guyz.smarty(a)gmail.com> wrote in message <i2f5cd$ajl$1(a)fred.mathworks.com>...
> Hi all,
>
> I am new to the MATLAB programming and am trying to learn now. I am stuck with a problem ( seems bigger for me, but may not be the same for you ;) ).
>
> Suppose A = [2 3 4] .... A vector
>
> and B = [2 1 2
> 3 4 2
> 3 5 4] ..... A Matrix
>
> now I want to check which elements of B are equal to each element of A individually, i.e., I want matrices like below:
>
> find(B==A(1))
> 1 0 1
> 0 0 1
> 0 0 0
>
> and
>
> find(B==A(2))
> 0 0 0
> 1 0 0
> 1 0 0
>
> and
>
> find(B==A(3))
> 0 0 0
> 0 1 0
> 0 0 1
>
> But I don't want to do it, three times manually. Also I don't want to do it with FOR loops (someone told me if I use FOR loops in MATLAB, then I am abusing it)
>
> Please give me some directions to follow.
>
> Any help will be appreciated.
>
> Thanks
From: someone on
"guyz Smarty" <guyz.smarty(a)gmail.com> wrote in message <i2f5cd$ajl$1(a)fred.mathworks.com>...
> Hi all,
>
> I am new to the MATLAB programming and am trying to learn now. I am stuck with a problem ( seems bigger for me, but may not be the same for you ;) ).
>
> Suppose A = [2 3 4] .... A vector
>
> and B = [2 1 2
> 3 4 2
> 3 5 4] ..... A Matrix
>
> now I want to check which elements of B are equal to each element of A individually, i.e., I want matrices like below:
>
> find(B==A(1))
> 1 0 1
> 0 0 1
> 0 0 0
>
> and
>
> find(B==A(2))
> 0 0 0
> 1 0 0
> 1 0 0
>
> and
>
> find(B==A(3))
> 0 0 0
> 0 1 0
> 0 0 1
>
> But I don't want to do it, three times manually. Also I don't want to do it with FOR loops (someone told me if I use FOR loops in MATLAB, then I am abusing it)

% Don't believe that. Sometimes for loops are faster than vectorized code.
% It depends on the size of the matricies and the available memory to MATLAB
% among other things. One solution (using one FOR loop & cell arrays):

A = [2 3 4];
B = [2 1 2;3 4 2;3 5 4];
% clear C
for index = 1:3
C{index} = zeros(3);
C{index}(find(B==A(index))) = 1;
end

>> C{1}

ans =

1 0 1
0 0 1
0 0 0

>> C{2}

ans =

0 0 0
1 0 0
1 0 0

>> C{3}

ans =

0 0 0
0 1 0
0 0 1

% Note the use of curley brackets for cell arrays.

>
> Please give me some directions to follow.
>
> Any help will be appreciated.
>
> Thanks
From: Jan Simon on
Dear guyz Smarty,

Important: Allocate the cell matrix C before the loop. This does not make a big difference your your tiny example. But it is a good idea to learn pre-allocation as early as possible.

A = [2 3 4];
B = [2 1 2;3 4 2;3 5 4];
C = cell(1, 3); % Pre-allocation
for index = 1:3
C{index} = (B==A(index));
end

Now the arrays in C are logical arrays. If you want them to be DOUBLE matrices (for reasons I cannot imagine), use:
C{index} = double(B==A(index)));

See also: 2nd output of ISMEMBER

Jan
From: Matt J on

If you do

Result=bsxfun(@eq,B,permute(A(:),[3,2,1]));

then each slice Result(:,:,i) will be the the result of find(B==A(i));