Using an if and for loop saving a part of a table

3 ビュー (過去 30 日間)
Zach
Zach 2023 年 6 月 5 日
回答済み: Shishir Reddy 2023 年 6 月 16 日
I have two matrices of tables. All the tables in y are in x. I am trying to pick out the times when x and y are happening the same time. I have it set up but I am not sure how to save part of the table. I think I am close but what am I missing? Can I do it like this: X_new{a,b}=X{a,1}(initial); or do I need to log the overlap then save the table afterwards.
Here is the code (the bolded part is the most critical part):
for a=1:Input1
for b=1:Input2
length_x=length(X{a}.time);
length_y=length(Y{a,b}.Time);
up=1;
for initial=1:length_X %Looking through data
if Y{a,b}.Time(count)==X{a,1}.time(initial)
X_new{a,b}=X{a,1}(initial);
count=count+1;
end
end
end
end

回答 (1 件)

Shishir Reddy
Shishir Reddy 2023 年 6 月 16 日
Hi Zach,
As per my understanding, you want to pick out the times when x and y are happening the same, in better possible way.
However there are a couple of minor issues in your code that need to be addressed.
  • The variable ‘length_X’ should be ‘length_x ‘(lowercase 'x') to match the variable assignment in the line above.
  • You need to initialize the variable ‘count’ before entering the loop. Add ‘count = 1;’ before the inner loop.
  • To save the part of the table where the times match, you can't directly assign it as X_new{a,b} = X{a,1}(initial). Instead, you can use the table function to create a new table with the desired rows.
Here’s the code using ismember() function:-
X_new = cell(Input1, Input2); % Initialize X_new cell array
for a = 1:Input1
for b = 1:Input2
x_times = X{a, 1}.time;
y_times = Y{a, b}.Time;
% Find the common times using ismember
common_times = x_times(ismember(x_times, y_times));
% Create a new table with the common times from X{a,1}
if ~isempty(common_times)
X_new{a, b} = X{a, 1}(ismember(x_times, common_times), :);
end
end
end
  • In this modified code, we first extract the time values from ‘X{a,1}’ and ‘Y{a,b}’ into ‘x_times’ and ‘y_times’, respectively. Then, we use the ‘ismember’ function to find the common times between ‘x_times’ and y_times. The result is stored in the ‘common_times’ variable.
  • Next, we check if there are any common times (‘~isempty(common_times)’), and if so, we use ‘ismember’ again to select the corresponding rows from ‘X{a,1}’ using ‘ismember(x_times, common_times)’. This gives us a new table with the rows that have matching times. We assign this new table to ‘X_new{a, b}’.
  • Note that ‘X_new{a, b}’ will be empty (i.e., ‘[]’) if there are no common times between x_times and y_times
  • Using ismember simplifies the code and avoids the need for nested loops and manual comparisons. It directly finds the matching times and extracts the corresponding rows from X{a,1} using logical indexing.
For further reference, kindly refer this link to know more about ‘ismember’
I hope this helps resolving the issue.

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

製品


リリース

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by