Hi,
I want to map two cell arrays.
Following the Containers.Map documentation it is possible : "Keys, specified as a numeric array, cell array of character vectors, or string array."
But if I try :
V = num2cell(rand(5,3));
K = num2cell([1 2; 3 4; 5 6; 1 2; 3 4]);
Then :
M = containers.Map(K,V);
I get the error :
Error using containers.Map
Unsupported key specified. See documentation for valid key types.
However K and V are both 5 x 1 cell arrays. What am I missing here ?
Thanks for help.
Nicolas
Edit : I would like each row of V matching with its key row in K.

7 comentarios

Nicolas Douillet
Nicolas Douillet el 11 de Dic. de 2019
Maybe cell array of character vectors only ?
Stephen23
Stephen23 el 11 de Dic. de 2019
"However K and V are both 5 x 1 cell arrays."
Yes... they are cell arrays of numeric arrays.
"What am I missing here ?"
Do you have cell arrays of character vectors (as the documentation requires)? (hint: no)
Nicolas Douillet
Nicolas Douillet el 11 de Dic. de 2019
Editada: Nicolas Douillet el 11 de Dic. de 2019
Ok I see, thanks Stephen.
I am trying to convert to strings using num2str, but now I get size troubles of course...
Do you see what I am trying to do ? Do you think it is possible, as far as you know ?
Thanks
Stephen23
Stephen23 el 11 de Dic. de 2019
Editada: Stephen23 el 11 de Dic. de 2019
"Do you see what I am trying to do ?"
Not really, as you did not explain anything about what you expect to get, and your code has unexplained "features" (e.g. cell arrays of numerics, non-unique keys).
Lets imagine that you get this to work: what value would you expect the key 1 to map to?
Nicolas Douillet
Nicolas Douillet el 11 de Dic. de 2019
Ok, it is a row match. V is the value set, and K is the key set.
You are right, there may be several values for a given key.
I want each row of V matching with its key row in K.
Stephen23
Stephen23 el 11 de Dic. de 2019
Editada: Stephen23 el 11 de Dic. de 2019
"You are right, there may be several values for a given key. "
In that case, if you use a key with multiple values do you expect MATLAB to return:
  1. one value (e.g. the last one defined using that key), or
  2. all of the values that have that key?
Please state your preference.
Nicolas Douillet
Nicolas Douillet el 11 de Dic. de 2019
Editada: Nicolas Douillet el 11 de Dic. de 2019
All the values of course.
Context is : I am creating an exact grid simplify point set function. In this function I want to replace every vertices of a cell (a vertex cluster then) by the closest vertex to the cell isobarycentre (not by the barycentre istelf which is the approximate version). A key set is 3D cell index (I simplified with only two integer in the previous example). First value set would be the original vertices set, and second one would be the barycentre set (smaller than the vertices set since there may be several vertices for a same barycentre). So in the end yes, I think I would need two maps, sharing the same key set.
Edit : I forgot to mention : I would like to do this without for loop.

Iniciar sesión para comentar.

 Respuesta aceptada

Stephen23
Stephen23 el 11 de Dic. de 2019

1 voto

"All the values of course."
Then you cannot use containers.Map: even if you manage to sort out everything else (those numeric inputs, rows, etc) it will only return one value (the last one) if a key has been defined multiple times:
>> K = { 'x', 'y', 'x'};
>> V = {'cat','sat','mat'};
>> C = containers.Map(K,V);
>> C('x')
ans =
mat
However you could easily use basic MATLAB indexing
>> V = rand(5,3)
V =
0.81472 0.09754 0.15761
0.90579 0.2785 0.97059
0.12699 0.54688 0.95717
0.91338 0.95751 0.48538
0.63236 0.96489 0.80028
>> K = [1,2;3,4;5,6;1,2;3,4]
K =
1 2
3 4
5 6
1 2
3 4
>> want = [3,4];
>> X = ismember(K,want,'rows');
>> V(X,:)
ans =
0.90579 0.2785 0.97059
0.63236 0.96489 0.80028

Más respuestas (0)

Categorías

Productos

Versión

R2019b

Etiquetas

Preguntada:

el 11 de Dic. de 2019

Editada:

el 11 de Dic. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by