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 (한국어)