I am trying to index from Cell array
Mostrar comentarios más antiguos
I have a nested cell array
pos{1X4}
>> {6830X1}{6824X1}{6834X1}{6826X1}{6842X1}{6840X1}{6836X1}{6844X1}{6837X1}{6833X1}
I am trying to index from cell array, but I am getting Unable to use a value of type cell as an index.
position_0{tr} = pos(time_0);
8 comentarios
dpb
el 9 de Dic. de 2022
Post a SMALL example that illustrates the storage and what you're trying to get -- it's certainly not clear how a 1x4 is related to what you show that has like 10 humongous vectors strung together...
Once it is known what the actual storage is, the addressing code will work whether there are 5 or 1E6 elements in the arrays, so make it easy on yourself and debug the small problem first.
First off, of course, is to reconsider how you got such a convoluted problem to solve in the first place--illustrating how this comes about may lead to a far more efficient and convenient storage scheme.
Imraan Ibrahim
el 9 de Dic. de 2022
It sounds like time_0 is a cell.
time_0 should to be positive integers or logical values
Kiran Isapure
el 9 de Dic. de 2022
Editada: Kiran Isapure
el 9 de Dic. de 2022
Imraan Ibrahim
el 9 de Dic. de 2022
Editada: Imraan Ibrahim
el 9 de Dic. de 2022
Have you tried the answer I gave?
More info is probably needed but, from the code that I'm seeing you don't need the 'UniformOutput'
time_0 = cellfun(@(t) find(t == 0,1), t, 'UniformOutput',false);
can be changed to:
time_0 = cellfun(@(t) find(t == 0,1), t);
and that should make time_0 a numerical array instead of a cell array, then you will be able to use time_0 to index the pos variable
Kiran Isapure
el 10 de Dic. de 2022
Imraan Ibrahim
el 10 de Dic. de 2022
Editada: Imraan Ibrahim
el 10 de Dic. de 2022
ok that sounds my suggestion has fixed one problem and uncovered another.
Without having the raw data infront of me, I'm making assumtions.
I think this new error is because pos is an array of 11 elements and one or more values in the time_0 array is greater than 11. And so the you get the Index exceeds the number of array elements. Index must not exceed 11. error
Kiran Isapure
el 12 de Dic. de 2022
Imraan Ibrahim
el 16 de Dic. de 2022
Editada: Imraan Ibrahim
el 16 de Dic. de 2022
I've just downloaded the raw data
pos =
1×11 cell array
{0×0 double} {6830×1 double} {6824×1 double} {6834×1 double} {6826×1 double} {6842×1 double} {6840×1 double} {6836×1 double} {6844×1 double} {6837×1 double} {6833×1 double}
time_0 =
1×11 cell array
{0×0 double} {[1002]} {[1002]} {[1002]} {[1002]} {[1002]} {[1002]} {[1002]} {[1002]} {[1002]} {[1002]}
Try the following:
position_0 = cellfun(@(p,t) p(t), pos, time_0, 'UniformOutput', false);
Bear in mind that the 1st index of position_0 will also be empty
position_0 =
1×11 cell array
{0×0 double} {[86.9711]} {[86.9943]} {[87.0181]} {[87.0761]} {[87.0062]} {[87.0405]} {[86.9515]} {[86.9467]} {[87.0836]} {[86.9525]}
Respuesta aceptada
Más respuestas (0)
Categorías
Más información sobre Logical en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!