在for 循环中取数列最后一个元素。

各位大佬早上好
今天我想取最后一个元素时,matlab总是报错:下标索引必须为正整数类型或逻辑类型。
以下为我的代码
a=[5 3 4 ;6 2 72; 71 13 5];
for i=1:3
[~,col_last]=find(a(i,:)>10);
b=col_last(end);
end
经过我思考觉得问题出在col_last(end)是吗,请问怎么解决呢,谢谢!

 採用された回答

daquxer
daquxer 2023 年 5 月 19 日

0 投票

问题在于你的find出来一个[]值,所以不能索引,代码改成
[row, colu] = find(a > 10);
b = arrayfun(@(x) colu(find(row == x, 1, 'last')), unique(row));

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeMATLAB についてさらに検索

タグ

質問済み:

2023 年 5 月 19 日

回答済み:

2023 年 5 月 19 日

Community Treasure Hunt

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

Start Hunting!