Finding index if exist
15 views (last 30 days)
Show older comments
Hello,
I am trying to check if specific data is inside my variable and if it does - extract it. If it does not exist, the script should just proceed.
if true
% code
end
for ww = 1:mfiles
idx_r02r(ww) = find(contains(Data_description{1,(ww)},'RO*_Right'));
if ~isempty(idx_r02r)
R02_r{ww} = Data_measured{1,ww}{1,idx_r02r};
R02_r = cell2mat(R02_r);
R02_r = reshape(R02_r, [t,mfiles])';
elseif isempty(idx_r02r)
end
end
the script gives me an error 'Unable to perform assignment because the left and right sides have a different number of elements.' already for the second row of my script. I expect for example a vector idx_r02r = [0 0 0 1] if the measurement R0* exists for the fourth test subject. I tried removing ww from the left side of
idx_r02r(ww) = find(contains(Data_description{1,(ww)},'RO*_Right'));
and it gives me an empty matrix but it overwrites the variable for each test subject.
Can you help me here?
0 Comments
Answers (1)
Jan
on 7 Nov 2018
Edited: Jan
on 8 Nov 2018
idx_r02r(ww) = find(contains(Data_description{1,(ww)},'R0*_Right'));
This can work only, if exactly 1 matching string is found. Otherwise you have a scalar on the left and either an empty array or a vector with more than 1 element. Maybe you want:
idx_r02r = contains(Data_description{1, ww}, 'R0*_Right');
idx_r02l = contains(Data_description{1, ww}, 'R0*_Left');
R02_r{ww} = [Data_measured{1,ww}{1,idx_r02r}];
R02_l{ww} = [Data_measured{1,ww}{1,idx_r02l}];
But this is guessing only, because I'm not sure what you want to achieve.
4 Comments
Jan
on 21 Nov 2018
I'm not sure, what the problem is. Maybe you want:
% Example - use the variable obtained in your code...
idx_r02r = logical([0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0])
idx_r02r = idx_r02r .* 1:numel(idx_r02r);
On the other hand: Why is the logical indexing not sufficient in your case? In the shown code, setting idx_r02r to the numerical indices, e.g. filled with zeros, should be either failing or it is a wast of time. Logical indexing is usually faster.
See Also
Categories
Find more on Resizing and Reshaping Matrices 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!