Indexing from equivalent rows in two matrices

1 view (last 30 days)
Hi all,
I am extract data from a structure based on a rule from one field, and another rule in the corresponding row of a second field. For that I have the following code:
for k= 1:size(MouseData, 2) % for each entry in the structure
for n= 1:numel(Behaviours1) %for each value in this cell in this array
% Create an index with the following rules:
% 1. If BehaviourType (first field), matches the value in the cell array
% 2. If the value in the 4th column of the second field (in the corresponding row), matches the value "NovMouse"
idx = find (strcmp(MouseData(k).BehaviourType, Behaviours1(n)) & ((MouseData(k).BehaviourData(:,4) == NovMouse))); % make an idx (index) where the behaviour type == behaviour (logical array of 0/1)
% Take that data (and so on......)
temp = MouseData(k).BehaviourType(idx);
NovMouseData(k).BehaviourType = vertcat(NovMouseData(k).BehaviourType, temp);
temp1 = MouseData(k).BehaviourData(idx, :);
NovMouseData(k).BehaviourData = vertcat(NovMouseData(k).BehaviourData, temp1);
end
end
I think I am going wrong in the line beginning with "idx", I don't think the code knows that I am asking it to find the corresponding rows. I know that I could create an if statement for this, but I worry that the code might take a long time to run if I do this?

Answers (1)

Raunak Gupta
Raunak Gupta on 28 May 2020
Hi,
One improvement I see in the code is you can remove
for n= 1:numel(Behaviours1)
Because strcmp also works when comparing a string with a cell array containing multiple strings. Here since the structures are stored in vector format you may not be able to remove outermost for loop and you are using k downwards so outermost for loop is required. I am assuming that Behaviours1 is a cell array containing strings and MouseData(k).BehaviourType is also a cell array because temp is calculated with some index idx. Rest all look ok to me.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!