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 Center および File ExchangeDatabase Toolbox についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by