# Extracting a specific row from two different variables

19 visualizaciones (últimos 30 días)
JAMIE DYSON el 22 de Jun. de 2023
Respondida: Kautuk Raj el 27 de Jun. de 2023
I have two different variables lets call them "time" and "type of fruit", I want to extract into separate tables the times when different fruits appear. In the end I would have a table that would contain all the times for apples in a variable, etc...
Additionally, if a fruit that has already been extracted appears further down in the table (after a different fruit) I would like a space in the final tables so I know when a fruit hasnt appeared on the row prior.
I am a novice to MATLAB, but any help or direction with this "question" would be much appreciated
##### 1 comentarioMostrar -1 comentarios más antiguosOcultar -1 comentarios más antiguos
dpb el 23 de Jun. de 2023
Not clear to me what your end object is here in how you would intend to use this and particularly the reason for including missing data in a field.
I'd ask why not create the full table (or timetable, maybe) that has both the fruit and the time from which you can use grouping variables to do anything you wish by fruit and/or time without making more variables; just use the data you already have to respond to whatever questions there are to be answered.

Iniciar sesión para comentar.

### Respuestas (1)

Kautuk Raj el 27 de Jun. de 2023
You can use MATLAB's logical indexing feature. Here is an example of how you can do this:
Assuming your data is stored in two arrays: "time" and "fruit_type".
First, find the unique fruit types in the "fruit_type" array using the "unique" function:
unique_fruits = unique(fruit_type);
% This will give you an array of the unique fruit types in the data.
Create a cell array to store the separate tables for each fruit type:
fruit_tables = cell(numel(unique_fruits), 1);
This creates a cell array with one cell for each unique fruit type.
Loop through the unique fruit types and extract the times for each fruit:
for i = 1:numel(unique_fruits)
fruit = unique_fruits(i);
fruit_times = time(fruit_type == fruit);
fruit_tables{i} = fruit_times;
end
This loop extracts the times for each unique fruit type and stores them in the corresponding cell of the "fruit_tables" array.
Add a space in the final tables if a fruit hasn't appeared in the previous row:
for i = 1:numel(unique_fruits)
fruit_table = fruit_tables{i};
for j = 2:numel(fruit_table)
if (fruit_table(j) - fruit_table(j-1)) > 1
fruit_table = [fruit_table(1:j-1); NaN; fruit_table(j:end)];
end
end
fruit_tables{i} = fruit_table;
end
This loop checks if there is a gap of more than one between the times in the current fruit table. If there is, it adds a NaN value to the table to indicate the gap.
At the end of this process, the "fruit_tables" cell array will contain separate tables for each fruit type, with gaps indicated where a fruit has not appeared in the previous row. You can access the tables for each fruit type using array indexing, for example:
apple_table = fruit_tables{1};
banana_table = fruit_tables{2};
Note that this is just an example implementation and the specific details of the data and desired output may require adjustments to this code.
##### 0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

### Categorías

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

R2021b

### Community Treasure Hunt

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

Start Hunting!

Translated by