Find the index of cell array containing numeric values
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
the cyclist
2014-4-17
编辑:the cyclist
2014-4-17
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 个评论
Stephen23
2014-12-21
编辑:Stephen23
2015-1-2
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
2014-12-28
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
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!