How can I create a new cell array from a existing array that only holds specific values?

47 次查看(过去 30 天)
Okay, so my problem is pretty basic but I just cant solve it. My basic Problem im trying to solve is as follow: I get different sets of data i want to plot, which means i have x and y data, but sometimtes i only have one set of data sometimes i have 4. I have written an for loop to plot every set in a different color etc.. Until now everyting works fine.
Now to my problem: Only a specific range of values are interesting for me, the ones from 1<=x<=4. I Still want to plot all data but only this range should be in color.
  1. I tried achivieng this directly in the plot info but it didnt work and found some similar question online where someone suggested to just split ur array. So im looking for the index in my x array where the value first goes over 1 and the index where it first get over 4. I need the index bc i need to split my y accordingly. First question here is. How can i just get every index? My data is not linear which means after my first value of 1.002 i may get another 0.998.. I would like to have all of the indices that have a value between 1 and 4.
idx_min{i} = {find(x_all{i}>=1,1)}
idx_max{i} = {find(x_all{i}<=4,1)}
This is what i do until now.. which means for each Set of Data created in the loop (x_all{i} with i the number of data setsif finds me the first index of an value above 1 and under 4. I tried combining the two for all values but i constantly get the invalif use of operator error.
2. But even if i have the first and last indicies i cant create a new cell array that contains all the values of my first array within my 2 indices. For calification i have an
example.
for i = 1:number_of_data_sets
set{i} = 1:number_of_data_sets
set{i} = (data.x(i)value data.y(i).value] %matches my x to my y values needed later on
x{i} = 1:number
y{i} = 1:number
x{i} = tyre{i}(:,1)
y{i} = tyre{i}(:,2)
idx_min{i} = {find(x{i}>=1,1)}
idx_max{i} = {find(x{i}<=4,1)}
x_relevant{i} = x{i}(idx_min{i}:idx_max{i}) %this should take my x array with the values from my minimum index to the maximum index but i get Undefined operator ':' for input arguments of type 'cell'.
Im pretty new to matlab and im getting consufesed with the index of my loop, the index of the arrays created in the loop and the index of my values.

采纳的回答

Stephen23
Stephen23 2020-11-26
编辑:Stephen23 2020-11-26
You are defining a scalar cell on the RHS and putting it inside another cell array on the LHS, giving you nested cell arrays. But you don't need nested cell arrays, you just need one cell array.
What you have now:
idx_min{i} = {find(x_all{i}>=1,1)}
% ^ ^ get rid of these
What you should have:
idx_min{i} = find(x_all{i}>=1,1);
  2 个评论
Patrick Petre
Patrick Petre 2020-11-26
编辑:Patrick Petre 2020-11-26
Holy, it actually half works now.. my new array is still empty because (I only now noticed) idx_max just says 1.
EDIT: OMFG sometimes i want to slap myself
Can you explain what the difference is when i write it without the {}
Stephen23
Stephen23 2020-11-26
编辑:Stephen23 2020-11-26
"Can you explain what the difference is when i write it without the {}"
Lets demonstrate with a cell array named C:
C = cell(1,2);
using {} on the LHS puts something into that cell array:
C{1} = [2,3,5];
Using {} around an expression puts that expression inside a scalar cell array. Here are some scalar cell arrays containing other arrays of different types and sizes (note that the cell array is always scalar):
{'blah'}
ans = 1x1 cell array
{'blah'}
{[2,3,5]}
ans = 1x1 cell array
{1×3 double}
{[]}
ans = 1x1 cell array
{0×0 double}
So when you do this:
C{2} = {[3,6,9]}
C = 1x2 cell array
{1×3 double} {1×1 cell}
you are putting a scalar cell array (that you define on the RHS) inside another cell array (on the LHS), thus giving you nested cell arrays. Look at what MATLAB is telling us about the content of C: the first cell contains a numeric vector, the second cell contains a scalar cell array.
Nested cell arrays can be useful in some situations, but not for what you are trying to do.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by