# Accessing values in a Cell Array using Index Values stored in another array

5 views (last 30 days)
Ben on 22 Sep 2020 at 0:04
Commented: Star Strider on 22 Sep 2020 at 11:27
Hi All,
I have some data stored in a 1x15 cell array. I am trying to access that data using index values stored in another array. The code my help to explain the problem.
I have a 1x15 array called NewVector
NewVector = [1.5 1.5 2.1 2.2 2.2 2.2 2.2 0.9 0.8 2.4 2.3 2.8 2.4 2.9 3.1]
I then create an array storing the indices of the values I want from NewVector. In this case values 2.8 and 2.9 (index position 12 and 14 respectively) .
index = find(NewVector > 2.7 & NewVector < 3.0) % Obatin index values and store
I then want to use these values to access data in my 1x15 cell array called NewSpeed i.e. I want to access values {1,12} and {1,14}.
So far I have the code below, but it doesn’t give the correct result…
l= length(index)
for x =1: length(index)
t = index(1:x)
CellVal(:,x) = NewSpeed{1,t}
end
Any help would be appreciated.
Thanks
Ben

sushanth govinahallisathyanarayana on 22 Sep 2020 at 0:20
You could use simple logical indexing, for example,
indexedValues=array(index);
where index is the result of NewVector > 2.7 & NewVector < 3.0
If this is from a cell array, you could try array{element}(index) while traversing the array in a loop.
If you want to avoid loops altogether, you can use cellfun, but that requires you to have newvector and index as cell arrays as well, you can easily convert these using mat2cell or num2cell.
Hope this helps.
Ben on 22 Sep 2020 at 0:39
Hi,
Thanks for the response. I'm not entirely clear on how I would do it using cellfun? Converting the arrays (newvector and index) is trivial.
NewVectorCell = mat2cell(NewVector,1)
IndexCell = mat2cell(index,1)
I'm not sure how this helps to achieve tthe end goal. Could you elaborate slightly on your answer? Thanks

Star Strider on 22 Sep 2020 at 1:49
That requires a bit of cell array gymnastics:
NewVector = [1.5 1.5 2.1 2.2 2.2 2.2 2.2 0.9 0.8 2.4 2.3 2.8 2.4 2.9 3.1];
NewVectorCell = mat2cell(NewVector,1);
index = cellfun(@(x)find(x > 2.7 & x < 3.0), NewVectorCell, 'Unif',0); % Obatin index values and store
SelectNewVectorCell = NewVectorCell{:}([index{:}])
producing:
SelectNewVectorCell =
2.8 2.9
.

Ben on 22 Sep 2020 at 3:56
Thanks,
This is almost correct; however, rather than storing the values of i.e 2.8 and 2.9 in the SelectNewVectorCell array I need to access cells {1,12} and {1,14} in another cell array called NewSpeed (this is a 1x15 cell array. So the final line would be
SelectNewVectorCell = NewSpeed{:}([index{:}])
However, that obviously does not work. I'm not sure how to solve this. You get the following error because fo the way the index values are stored.
Expected one output from a curly brace or dot indexing expression, but there were 2
results.
Error in test (line 50)
SelectNewVectorCell = NewSpeed{[index{:}]}(:)
NewSpeed is a cell array in the following from :
NewSpeed = {[1;5;3;4;2] [3;4;5;2;1] [1;5;3;4;2] [3;4;5;2;1] [1;5;3;4;2] [3;4;5;2;1] [1;5;3;4;2] [3;4;5;2;1] [1;5;3;4;2] [3;4;5;2;1] [1;5;3;4;2] [3;4;5;2;1] [1;5;3;4;2] [3;4;5;2;1] [3;4;5;2;1]};
Star Strider on 22 Sep 2020 at 11:27
My code runs without error and produces the correct result. The ‘NewSpeed’ (1x15) cell array of (1x5) double vectors was never in the original problem.
Indexing into it is straightforward cell array indexing:
SelectNewSpeed = NewSpeed([index{:}])
with:
Result = cell2mat(SelectNewSpeed) % Works Here Because The Vectors Are The Same Size
producing
Result =
3 3
4 4
5 5
2 2
1 1
Those two vectors (elements 12 and 14) are the same. The code retrieved them correctly, as can be demonstrated by altering one number from each of those vectors to demonstrate that in that event they’re different.