Info
This question is closed. Reopen it to edit or answer.
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
How to convert 1x24 cell with (dif_val x 3) to Matrix [all_dif_val x 2]
1 view (last 30 days)
Show older comments
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 Comments
Answers (1)
Ameer Hamza
on 23 May 2020
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 Comments
Nik Rocky
on 24 May 2020
Edited: Nik Rocky
on 24 May 2020
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
on 24 May 2020
See cellfun(). You can apply your interpolation function to each cell individually.
Nik Rocky
on 24 May 2020
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
on 24 May 2020
Why not specify to extrapolation with interp1
fun = @(x,y)interp1(x,y,t_ref,'linear','extrap');
Nik Rocky
on 24 May 2020
Edited: Nik Rocky
on 24 May 2020
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
on 24 May 2020
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
on 24 May 2020
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
on 24 May 2020
Edited: Nik Rocky
on 24 May 2020
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
on 25 May 2020
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
on 25 May 2020
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:
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
on 25 May 2020
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
See Also
Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- 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)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)