pulling non-consistent arrays out of a structure
조회 수: 3 (최근 30일)
이전 댓글 표시
Hello, I am trying to pull out arrays from a structure previously defined in my code to then find the median behavior, so I can plot alongside my individual object's behaviors. The overall goal is to track median behavior of all obects over the time of the experiment.
Let me create an example via words, as I do not know how to recapitulate via code. Object 1-5 were detected over the whole length of the experiment, so they have all data points. Object 6-9 had varying degrees of detection, but were detected for more than 70% of the experiment, and so are considered 'tracked well enough to retain'.
The problem is when attempting horzcat or any way to pull data out of the structure, due to the inconsistent size, matlab has an error. The overall idea I have to use a 'full detection' object to compare all other objects to, and if an image frame wasn't found in the array of a certain object, to fill the row that should have been that with NaN. Then all arrays would be consistent, and I could pull them out of their individual sections within the structure to then collectively median.
While I don't know how to do most of that, attached is the structure, and what I would like to do with the structure once the NaN is inserted into different objects. If there is a better or more efficient way to do this, perhaps without even inserting the NaN, please let me know!!
load 'DataStruct.mat'
% Note that Data.m was used to contruct two different populations, Data.PC3
% and .MDA, disregard Data.m at this point
%once NaN inserted, separate x and y of each object within the structure
%for easier extraction
idxPC3 =
5 6 7 8 9
for i = 1:length(idxPC3)
for j = 1:length(Data.m{idxPC3(i)}.n)
if i== 1
Data.PC3.x = Data.m{idxPC3(i)}.n{1,j}(:,1);
Data.PC3.y = Data.m{idxPC3(i)}.n{1,j}(:,2);
else
Data.PC3.x = horzcat(Data.PC3.x, Data.m{idxPC3(i)}.n{1,j}(:,1));
Data.PC3.y = horzcat(Data.PC3.y, Data.m{idxPC3(i)}.n{1,j}(:,2));
end
end
end
MedPC3= median(Data.PC3.y, 2) %the median,2 is used for median behavior across time points, not at the median time point
plot(Data.PC3.x, MedPC3) %plot the median behavior with respect to time (x data points)
댓글 수: 0
채택된 답변
Matt J
2024년 3월 22일
편집: Matt J
2024년 3월 22일
Here's a way you can extract and concatenate all the x,y data. However, I don't understand what you are trying to do with the median operation. Since x(:,t) and y(:,t) both vary with t, taking the median of y(k,t) across t will not give a median value corresponding to a well-defined x-coordinate. You would need to do some sort of interpolation of the x,y data onto a common x-axis.
load DataStruct
idxPC3 =5:9;
data=Data.m(idxPC3);
data=[data{:}];
data=[data.n];
L=max(cellfun(@height,data));
for i=1:numel(data)
data{i}(end+1:L,:)=nan;
end
data=cell2mat(data);
x=data(:,1:2:end);
y=data(:,2:2:end);
whos x y
댓글 수: 8
Stephen23
2024년 5월 14일
"why was this moved and the accepted answer changed?"
To give the appropriate credit to Matt J for their effort volunteering their time helping you, because as you stated: "This answer was inspired and modified from an orginal answer by @Matt J! "
"the previous answer did not completely and explicitly answer the question..."
It is very common that a first attempt at answering question does not work, and that only after some discussion (e.g. in the comments) that a suitable resolution is found. In such cases, the answer that triggered the resolution is commonly accepted in recognition and support of the user who intiated that approach.
추가 답변 (1개)
Steven Lord
2024년 5월 14일
If you're using release R2023b or later, the resize function may be of use. Let's make some sample data.
data = {(1:3).', (4:8).', [9; 10], (11:17).'}
Let's see what the tallest vector is among those stored in the cells of data.
L=max(cellfun(@height,data))
Now we resize each vector in data to that height, filling with NaN. Since the output of resize is not scalar, we need to store them back in a cell array using the UniformOutput name-value argument to cellfun.
data = cellfun(@(x) resize(x, L, FillValue=NaN), data, UniformOutput=false)
What does each cell looks like?
celldisp(data)
Finally we can concatenate them all together.
D = [data{:}]
댓글 수: 2
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!