Info
此问题已关闭。 请重新打开它进行编辑或回答。
How to convert 1x24 cell with (dif_val x 3) to Matrix [all_dif_val x 2]
1 次查看(过去 30 天)
显示 更早的评论
Hello dear community,
I have one Cell{} 1x24 cell. Cell{1,1} have a 24x3 matrix, Cell{1,2} have a 608x3 matrix, ....., Cell{1,24} have a 12x3 matrix.
I need just one matrix with values from Traj{1,i}(:,1) and Traj{1,i}(:,2)
Both values should be together and be sorted by first values (times), example:
Cell{1,1} =
1 78
3 47
5 40
Cell{1,2} =
7 76
4 24
Result_Matrix_wanted =
[1 78; 3 47; 4 24; 5 40; 7 76]
If I do a plot , it works by:
for u = 1:length(Cell)
hold on;
p5 = plot(Cell{1,u}(:,1),Cell{1,u}(:,2),'b--o','Color','c');
end
But I can't create a matrix with this method. Can you help me please? Thank you in advice!
0 个评论
此问题已关闭。
回答(1 个)
Ameer Hamza
2020-5-23
Try this example
C{1,1} = ...
[1 78
3 47
5 40];
C{1,2} = ...
[7 76
4 24];
A = vertcat(C{:});
A = A(:,1:2); % only get first two column
A = sortrows(A, 1);
14 个评论
Nik Rocky
2020-5-24
编辑:Nik Rocky
2020-5-24
Hello, I have another error now.
My Cells have often the same time values. But I have to interpolate the values to separate x -axis. Example:
Traj{1}(1:10,1)
ans =
0.1858
0.2323
0.2788
0.3253
0.3718
0.4183
0.4648
0.5114
0.5579
0.6044
Traj{3}(1:15,1)
ans =
0.1858
0.2323
0.2788
0.3253
0.5114
0.5579
0.6044
0.6509
0.6974
0.7439
0.9300
0.9765
1.0230
1.0695
1.1160
I want to interpolate it with:
t_ref = 0:0.01:30; %3001 values
of couse of same x-values on different cells, I cant use vertcat - in this case I get:
F0_t(1:10)
ans =
0.1858
0.1858
0.2323
0.2323
0.2788
0.2788
0.3253
0.3253
0.3718
0.4183
So I want to interpolate directly cells (withou converting to vectors/matrix) and get new y-values with:
for l = 1:length(Traj)
new_traj = interp1(Traj{l}(:,1),Traj{l}(:,2),t_ref,'cubic','extrap');
end
So I get array with 3001 same values (last value of last cell Traj{1}("last",1). How can I do it right? Thank you!
Ameer Hamza
2020-5-24
See cellfun(). You can apply your interpolation function to each cell individually.
Nik Rocky
2020-5-24
Thank you again! Yes, this is that I want - to have interpolated Cell1, Cell2,....Celln
If I look example on MATLAB-Descripion
A = cellfun(func,C1,...,Cn)
and I find example and modified this:
X = {[1,4,8,10],[2,6,8,9]};
Y = {[1,2,6,7],[5,9,7,6]};
t_ref = 0:0.01:30;
fun = @(x,y)interp1(x,y,t_ref,'linear');
C = cellfun(fun,X,Y,'uni',0);
I tryed to start this, but I get NaN inside of C
if I try to tranfer to my code:
for l = 1:length(Traj)
new_traj = cellfun(@((Traj{l}(:,1)),(Traj{l}(:,2)))interp1((Traj{l}(:,1)),(Traj{1}(:,2)),t_ref));
end
What i do wrong?
Ameer Hamza
2020-5-24
Why not specify to extrapolation with interp1
fun = @(x,y)interp1(x,y,t_ref,'linear','extrap');
Nik Rocky
2020-5-24
编辑:Nik Rocky
2020-5-24
fun = @(Traj{1}(:,1),Traj{1}(:,2))interp1(Traj{1}(:,1),Traj{1}(:,2),t_ref,'linear',"extrap");
fun = @(Traj{1}(:,1),Traj{1}(:,2))interp1(Traj{1}(:,1),Traj{1}(:,2),t_ref,'linear',"extrap");
↑
Error: Invalid expression. When calling a function or indexing a variable, use parentheses. Otherwise, check for mismatched delimiters.
can I use for x: Traj{1}(:,1) and y: Traj{1}(:,2) ?
Nik Rocky
2020-5-24
I make a steps:
x = Traj{1}(:,1);
y = Traj{1}(:,2);
fun = @(x,y)interp1(x,y,t_ref,'linear','extrap');
%%%Okay
C = cellfun(fun,x,y,'uni',0);
Error using cellfun
Input #2 expected to be a cell array, was double instead.
Ameer Hamza
2020-5-24
Try this
t_ref = 0:0.01:30; %3001 values
fun = @(x,y) interp1(x,y,t_ref,'linear','extrap');
cellfun(@(x) [t_ref.' fun(x(:,1), x(:,2)).'], Traj, 'Uni', 0)
Nik Rocky
2020-5-24
编辑:Nik Rocky
2020-5-24
You are genius, thank you very much!
I used:
x = Traj{1}(:,1);
y = Traj{1}(:,2);
fun = @(x,y) interp1(x,y,t_ref,'linear','extrap');
C = cellfun(@(x) [t_ref.' fun(x(:,1), x(:,2)).'], Traj, 'Uni', 0);
I get for C: 1x24 cell with 1x6002 interpolated double in format t1,t2,t3....tn,y1,y2,y3,,,yn
Now I have to split cells in two variables or make cells with 2x3001 double.
Can you describe me, how did you know how to feel this function - I was not able to read this on cellfun- or interp1-description on MATLAB manual page!
@(x) [t_ref.' fun(x(:,1), x(:,2)).'], Traj,
its looks for me like a magic!
Ameer Hamza
2020-5-25
Nikita, Are you running exactly these lines of code? Because they should already output each cell of size 3001x2. Also, the function cellfun is used to apply a function to each element of a cell array. It can be considered a compact for-loop. The cellfun line in my code id equivalent to
C = cell(1,numel(Traj));
for i=1:numel(Traj)
C{i} = [t_ref.' fun(Traj{i}(:,1), Traj{i}(:,2)).'];
end
in my code, I just used an anonymous function to write the same thing. You can define any function you want to use with cellfun.
Nik Rocky
2020-5-25
Hello dear Ameer,
I'm sorry for late answer! Your code works, I made a mistake to convert:
t_ref = 0:0.01:30;
%t_ref = t_ref'; <---
Now everything works, but still not like I need. I'm sorry, I think my description of problem was not enough to understand what I need!
I create a diagram now, maybe so you can understand:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/300518/image.png)
So, after creating t_ref values (3000 values from 0 to 30sek) i do interpolation of m Motors (up to four).
t_ref = 0:0.01:30;
m1 = interp1(t,v1,t_ref,'pchip','extrap');
m2 = interp1(t,v2,t_ref,'pchip','extrap');
m3 = interp1(t,v3,t_ref,'pchip','extrap');
m4 = interp1(t,v4,t_ref,'pchip','extrap');
After that I want to do a same with Traj{n}, but this interpolation has to be done while small range of t_Traj{n} and not the whole t_ref.
Then, I want calculate a True Positive/True Negative/False Positive/False Negative (I think False Negative make no sense because we have a discret values and amount of "holes" depends on from sampling grid).
Nik Rocky
2020-5-25
I get it!
Traj_interpl = cell(1,numel(Traj));
for i=1:numel(Traj)
minValue = min(Traj{i}(:,1));
maxValue = max(Traj{i}(:,1));
indexesInRange = t_ref > minValue & t_ref < maxValue;
t_Traj = t_ref(indexesInRange);
fun = @(x,y) interp1(x,y,t_Traj,'linear','extrap');
Traj_interpl{i} = [t_Traj.' fun(Traj{i}(:,1), Traj{i}(:,2)).'];
end
hold on
for u = 1:length(Traj_interpl)
hold on;
p6 = plot(Traj_interpl{u}(:,1),Traj_interpl{u}(:,2),'b--o','Color','c');
end
此问题已关闭。
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)