Find the index of cell array containing numeric values
1 view (last 30 days)
Show older comments
Hi!I have a cell array=YTLim, which contains at each cell the measurements of each for significant wave height Hs(numeric values).Each cell has 2920=365*8 or 2928=366*8 measurements,it depends on the year.So first cell has the values
YTLim{1}=[1,2,3,...,2920]
YTLim{2}=[2921,2922,2923,...,5840] etc
I have,also, a vector that contains the spot of some measurements. For example:
maxpos=[242;365;...;8555]
I need to find a way to take the index of the cell containing the vector's value...Is that possible?
I need something like that:
YS=[1;1;2;...;10;11;11]
0 Comments
Accepted Answer
the cyclist
on 17 Apr 2014
Edited: the cyclist
on 17 Apr 2014
Here is a miniature example that I believe does what you've asked for:
YTLim{1}=[1,2,3,4];
YTLim{2}=[5,6,7,8];
YTLim{3}=[9,10,11,12,13];
maxpos=[1,3,7,13];
c = cellfun(@(x)(ismember(maxpos,x)),YTLim,'UniformOutput',false);
[YS,~] = find(reshape([c{:}],numel(maxpos),[])')
I have to admit the code is compact and maybe a bit obscure. But if you take the time to examine each piece, I'm confident you can see what each individual command is doing.
5 Comments
Stephen23
on 21 Dec 2014
Edited: Stephen23
on 2 Jan 2015
James: If the data arrays are contained in a cell array, then there you basically have two choices for comparing these with some scalar value:
- in a loop
- using cellfun
The loop may be a bit faster, but cellfun can produce much neater code.
Some other points in your code need improving though:
Note that because you are comparing two scalars there is no reason to use ismember at all: you can use logical relational operators for a scalar with any sized array:
idx = Array == Scalar;
If you are so concerned about speed why do you call randi 10,000 times in a loop? This indicates some reading up on MATLAB vectorization is required. For example, it could be generated faster and tidier using num2cell:
num2cell(randi(10,100,1))
This leads on to the most important question: why are you storing scalar values in a cell array? Unless you have a good reason to do this, you should keep your data in a numeric array, which then makes all the other operations suddenly much, much easier to perform... For example, all of your code could be reduced to:
A = randi(10,100,1);
B = A==3;
James
on 28 Dec 2014
The only problem is that I have uneven arrays of different sizes. Hence the need for cells. However, using the logical == operator instead of the ismember function definitely gives better results. In fact it cut the time by about 3/4. I'm a little embarrassed I didn't think of it. Thank you for helping me out.
I'm actually writing my own Q-hull algorithm. This is for determining if a point is in the outside set of the current faces. I know MATLAB has its own Q-hull method built into the delaunayTriangulation function but I wanted to make my own.
Thanks, James Peters
More Answers (0)
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!