I need to compare elements that come from different tables
5 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
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)
EQID_inter(1)
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
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)
% now Bnames_inter.Level is numeric, and the comparison works:
i = 1;
EQID_inter(i) == Bnames_inter.Level(i)
2 个评论
Voss
2024-8-28
You're welcome! Any questions, let me know. Otherwise, please "Accept" this answer. Thanks!
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Database Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!