![]() |
单元到矩阵匹配/映射/单元操作(MATLAB)
我无法使用下面的数据找到与finalAnswer等效的字符串。请,我不能使用if / for循环!最终答案是首选,每个元素都作为数组(即mainData的格式)
mainData = {'IBM' [201] [1] ; 'GE' [403] [1] ; 'MSFT' [502] [3] ; 'GM' [101] [2] } ; finalAns = [ 101 2 0.5; 403 1 0.6 ] ; %% I tried doing this -> temp = cell2mat(mainData(:,[2 3])) ; tf = ismember(temp, finalAns(:,[1 2],'rows') ; secIDs = mainData(tf) ; [B]回答:[/B] 为了得到每行中的条目mainData匹配那些在finalAns (基于最后两列mainData和前两列finalAns ),并让他们以相同的顺序,它们出现在finalAns ,到最后附加了finalAns列,您可以执行以下操作: >> temp = cell2mat(mainData(:,2:3)); >> [isThere,index] = ismember(finalAns(:,1:2),temp,'rows'); >> output = [mainData(index(isThere),:) num2cell(finalAns(isThere,3))] output = 'GM' [101] [2] [0.5000] 'GE' [403] [1] [0.6000] 输出是2×4单元格数组,每个值在单独的单元格中。如果要在向量中收集最后三列,则可以用以下内容替换上面的output计算: >> temp = [temp(index(isThere),:) finalAns(isThere,3)]; >> output = [mainData(index(isThere),1) num2cell(temp,2)] output = 'GM' [1x3 double] 'GE' [1x3 double] 请注意,现在您有了一个2×2单元格数组,其中第二列中的单元格包含1×3的双精度数组。 [url=https://stackoverflow.com/questions/4442550]更多&回答...[/url] |
所有时间均为北京时间。现在的时间是 01:05。 |
Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.