I need to compare elements that come from different tables

조회 수: 1 (최근 30일)
Ivan
Ivan 2024년 8월 27일
댓글: Voss 2024년 8월 28일
I am developing a code and have used the 'fitlme' tool to perform a mixed-effects regression. Once the regression is done, a table named 'Bnames_inter' is generated, which has a column named 'Level' where numbers are stored that I later need to compare with the numbers from another table. I believe my problem lies in the nature of the variables I want to compare, and that's why I am asking for help.
Here are the lines of code I am working on.
Nature of Bnames_inter.Level(1)
ans =
1×1 cell array
{'6000038'}
Nature of EQID_inter(1)
ans =
6000152
delta_inter = [];
for i = 1:length(eta_inter)
if EQID_inter(i) == Bnames_inter.Level(i)
delta_inter = [delta_inter, res_inter_3(i) - eta_inter(i) - c0_inter];
end
end

채택된 답변

Voss
Voss 2024년 8월 27일
EQID_inter is a numeric array.
Bnames_inter.Level is a cell array (whose first element contains the character vector '6000038').
You cannot compare a numeric array to a cell array.
Example:
% variables like yours, as far as I can tell:
Bnames_inter = table({'6000038';'6000039';'6000040'},'VariableNames',{'Level'});
EQID_inter = [6000152; 6000153; 6000154];
% confirming that these are the same as you show in the question:
Bnames_inter.Level(1)
ans = 1x1 cell array
{'6000038'}
EQID_inter(1)
ans = 6000152
try % try to compare a numeric array to a cell array
i = 1;
EQID_inter(i) == Bnames_inter.Level(i)
catch err % can't do it, show the error message (see below)
disp(err.message)
end
Undefined function 'eq' for input arguments of type 'cell'.
If you make Bnames_inter.Level a numeric array, then the comparison will work. To do that, make appropriate modifications to the code that creates Bnames_inter, or leave that code alone and convert Bnames_inter.Level after the fact.
The following conversion may or may not work (I don't know because I don't have your actual variables):
format long g % format for display purposes only
% convert Bnames_inter.Level from cell array of character
% vectors (presumed) to numeric array using str2double:
Bnames_inter.Level = str2double(Bnames_inter.Level)
Bnames_inter = 3x1 table
Level _______ 6000038 6000039 6000040
% now Bnames_inter.Level is numeric, and the comparison works:
i = 1;
EQID_inter(i) == Bnames_inter.Level(i)
ans = logical
0
  댓글 수: 2
Ivan
Ivan 2024년 8월 27일
Thank you very much! it worked
Voss
Voss 2024년 8월 28일
You're welcome! Any questions, let me know. Otherwise, please "Accept" this answer. Thanks!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Database Toolbox에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by