Reputation: 300
There is a cell
array in MATLAB
B =
[ 1708] [ 2392] '+'
[ 3394] [ 3660] '+'
[ 5490] [ 5743] '+'
[ 7555] [ 7809] '-'
[ 9256] [ 9509] '-'
[12878] [15066] '-'
[16478] [17458] '-'
and another cell
array
C =
[4]
[7]
[1]
[6]
[2]
[5]
[3]
I want to replace the values in C
with the values in B{...,3}
such that C
becomes
C =
'-'
'-'
'+'
'-'
'+'
'-'
'+'
How can I do that in MATLAB? I currently did this but got error
>> C(C == 'x') = B
Undefined function 'eq' for input arguments of type 'cell'.
Upvotes: 0
Views: 917
Reputation: 30579
Horizontal concatenation ([]
) with comma-separated list cell array output ({:}
) gives a direct way to index the appropriate rows in B
:
Cnew = B([C{:}],3)
Upvotes: 3
Reputation: 46365
You could try
C = cellfun(@(x)B(x,3),C);
This addresses the problem you were seeing with C
no longer being a cell array - note the subtle difference between B{}
and B()
.
Upvotes: 2
Reputation: 36710
Basic indexing, to get the elements of X
in the order a=[1,3,2,4]
use X(a)
. Indices are matrices, thus a conversion is needed, nothing else.
B(cell2mat(c),3)
Upvotes: 1