Finding index if exist

13 Ansichten (letzte 30 Tage)
KDRA
KDRA am 7 Nov. 2018
Kommentiert: Jan am 21 Nov. 2018
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?

Antworten (1)

Jan
Jan am 7 Nov. 2018
Bearbeitet: Jan am 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 Kommentare
KDRA
KDRA am 21 Nov. 2018
Hey Jan,
sorry for not being very specific in terms of MatLab language.
Indeed, I am trying to find out if any element of the cell string Data_description is 'RO*_Right'. idx is actually my Idx_r02_r and it specifies the indexes of the Data_description where 'RO*_Right' is found. Next step is extracting the data from Data_measured which have the same index as Idx_r02_r.
Thanks for pointing out the zeros and Oh confusion. I corrected it in my script.
I tried your solution with 'any' command. My script runs now and looks like this now:
for ww = 1:mfiles
idx_r02r = contains(Data_description{1,ww},'R0*_Right');
idx_r02l = contains(Data_description{1,ww},'R0*_Left');
if any(idx_r02r)
R02_r{ww} = Data_measured{1,ww}{1,idx_r02r};
R02_r = cell2mat(R02_r);
R02_r = reshape(R02_r, [t,mfiles])';
if any(idx_r02l)
R02_l{ww} = Data_measured{1,ww}{1,idx_r02l};
R02_l = cell2mat(R02_l);
R02_l = reshape(R02_l, [t,mfiles])';
end
end
end
But this doesn't help much as now it gives me logical values, not indexes (idx_r02r = 1×20 logical array 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0).
What I need is an index of a non zero element so in this case it would be idx_r02r = 1×20 logical array 0 0 0 0 0 0 0 0 9 0 0 0 0 0 0 0 0 0 0 0 0 because the non-zero element is in 9th column.
Jan
Jan am 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.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Resizing and Reshaping Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by