필터 지우기
필터 지우기

search for the position of a number inside a cell (cell with existence of empty rows)

조회 수: 2 (최근 30일)
I am trying to look for each row of 'N' inside a cell.
I am using 'find' but it doesn't seem to work.
I should identify, in the cell, the rows of N. In this case, row 3 and row 19.
parameters = importdata("parameters.mat");
N = [parameters{3,1}; parameters{19,1}];
tt = 1;
for t = 1:height(parameters)
[r,c] = find(parameters{t,1}(1,1) == N(tt,1) & ...
parameters{t,1}(1,2) == N(tt,2) & ...
parameters{t,1}(1,3) == N(tt,3) & ...
parameters{t,1}(1,4) == N(tt,4));
end

채택된 답변

Star Strider
Star Strider 2024년 2월 10일
Use ismember (since you have the exact values, otherwise ismembertol) and any (here using the default column orientation, dimension = 1) to be inclusive, with cellfun (that loops internally) —
load('parameters')
% whos
parameters
parameters = 21x1 cell array
{'parameters' } {[-23.7557 -202.2222 -189.4345 1.4501]} {[-27.2523 -210.5503 -172.2241 1.2088]} {0x0 double } {0x0 double } {0x0 double } {0x0 double } {0x0 double } {0x0 double } {0x0 double } {0x0 double } {0x0 double } {0x0 double } {0x0 double } {0x0 double } {0x0 double }
N = [parameters{3,1}; parameters{19,1}]; % 'Target' Values
idx = cell2mat(cellfun(@(x)any(ismember(x,N)), parameters, 'Unif',0)); % Logical Index Vector
Result_Index = find(idx) % Numeric Index
Result_Index = 2x1
3 19
Result = parameters(idx) % Result Values
Result = 2x1 cell array
{[-27.2523 -210.5503 -172.2241 1.2088]} {[-31.3830 -213.5605 -165.2437 1.4301]}
There are likely other ways to do this as well.
.

추가 답변 (1개)

VBBV
VBBV 2024년 2월 10일
편집: VBBV 2024년 2월 10일
Use a | instead of &. OR in place of AND
  댓글 수: 2
Alberto Acri
Alberto Acri 2024년 2월 10일
편집: Alberto Acri 2024년 2월 10일
I need to know if N(1,:) is inside 'parameters'
VBBV
VBBV 2024년 2월 10일
Using for loop it can be like this
p = load("parameters.mat");
N = [p.parameters{3,1}; p.parameters{19,1}]
N = 2×4
-27.2523 -210.5503 -172.2241 1.2088 -31.3830 -213.5605 -165.2437 1.4301
tt = 1;
for t = 1:height(p.parameters)-1
K = p.parameters{t+1,1};
if ~isempty(K)
if tt == 1 % N(1,:) inside parameter
[r,c] = find((K(1) - N(tt,1)) < 1e-3 & ...
(K(2) - N(tt,2)) < 1e-3 & ...
(K(3) - N(tt,3)) < 1e-3 & ...
(K(4) - N(tt,4)) < 1e-3)
else % N(2,:) inside parameter
tt = tt + 1;
[r,c] = find((K(1) - N(tt,1)) < 1e-3 & ...
(K(2) - N(tt,2)) < 1e-3 & ...
(K(3) - N(tt,3)) < 1e-3 & ...
(K(4) - N(tt,4)) < 1e-3)
end
end
end
r = [] c = []
r = 1
c = 1
r = [] c = [] r = [] c = []
You can try using ismember function to vaoid for loop

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by