pulling non-consistent arrays out of a structure

3 visualizaciones (últimos 30 días)
Nicholas Scott
Nicholas Scott el 22 de Mzo. de 2024
Comentada: Nicholas Scott el 14 de Mayo de 2024
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)

Respuesta aceptada

Matt J
Matt J el 22 de Mzo. de 2024
Editada: Matt J el 22 de Mzo. de 2024
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
Name Size Bytes Class Attributes x 79x133 84056 double y 79x133 84056 double
  8 comentarios
Stephen23
Stephen23 el 14 de Mayo de 2024
"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.
Nicholas Scott
Nicholas Scott el 14 de Mayo de 2024
Editada: Nicholas Scott el 14 de Mayo de 2024
@Stephen23 ok. That was the purpose of mentioning them and their response (to give credit and thank), but if that's the standard, onlookers may need to be edjucated to look at comments. I typically find comments to be long, spurious to the topic, or random folks that want their own question to be answered instead of creating a new thread, so after many instances of wasted time on various topics, I have learned to typically disregard comments, especially if it has 5+ comments in the comment section.
Hence burying the completely polished answer is creating a disservice to folks that need help for specific topics like this, just in an effort to attribute an answer to someone that was mentioned and acreddited for their input. If this comes off as hostile, it is not my intention; I am looking at this from the perspective of helping people find a complete answer quickly and accurately as paramount over redistributing credit when the person was mentioned within the more polished answer.

Iniciar sesión para comentar.

Más respuestas (1)

Steven Lord
Steven Lord el 14 de Mayo de 2024
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).'}
data = 1x4 cell array
{3x1 double} {5x1 double} {2x1 double} {7x1 double}
Let's see what the tallest vector is among those stored in the cells of data.
L=max(cellfun(@height,data))
L = 7
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)
data = 1x4 cell array
{7x1 double} {7x1 double} {7x1 double} {7x1 double}
What does each cell looks like?
celldisp(data)
data{1} = 1 2 3 NaN NaN NaN NaN data{2} = 4 5 6 7 8 NaN NaN data{3} = 9 10 NaN NaN NaN NaN NaN data{4} = 11 12 13 14 15 16 17
Finally we can concatenate them all together.
D = [data{:}]
D = 7x4
1 4 9 11 2 5 10 12 3 6 NaN 13 NaN 7 NaN 14 NaN 8 NaN 15 NaN NaN NaN 16 NaN NaN NaN 17
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
  2 comentarios
Matt J
Matt J el 14 de Mayo de 2024
Editada: Matt J el 14 de Mayo de 2024
But Steve's solution simply pads the data with trailing NaNs. In your comment to my original answer, you said that this is not what you want.
Nicholas Scott
Nicholas Scott el 14 de Mayo de 2024
You are correct, I misunderstood Steve's code, it does not insert the NaNs in the middle of data if a middle data piece is missing. It does not address the specific question we wished to answer. Thanks yet again @Matt J :).

Iniciar sesión para comentar.

Categorías

Más información sobre Logical en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by