Info

This question is closed. Reopen it to edit or answer.

How to convert 1x24 cell with (dif_val x 3) to Matrix [all_dif_val x 2]

1 view (last 30 days)
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!

Answers (1)

Ameer Hamza
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
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
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

Community Treasure Hunt

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

Start Hunting!