Finding index if exist
16 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(1 个)
Jan
2018-11-7
编辑:Jan
2018-11-8
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 个评论
Jan
2018-11-21
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!