find row and column of a value in cell array

1 回表示 (過去 30 日間)
NA
NA 2020 年 4 月 11 日
編集済み: NA 2020 年 4 月 11 日
I have A matrix of dimension 10*10. C is linear index of non zero element in A
C = {[11],[33;44;54;55],[77;87;98]};
I want to find row and column of linear index in cell array.
result should be
row_col = {[1,2],[3,4; 4,5; 4,6; 5,6],[7,8;7,9;8,10]}

採用された回答

Ameer Hamza
Ameer Hamza 2020 年 4 月 11 日
編集済み: Ameer Hamza 2020 年 4 月 11 日
C = {[11],[43;54;63;64],[87;97;108]};
A = rand(10);
result = cellfun(@(x) {ind2sub2(size(A), x)}, C);
function y = ind2sub2(sz, ind)
[r,c] = ind2sub(sz, ind);
y = [r(:) c(:)];
end
Result:
>> result{1}
ans =
1 2
>> result{2}
ans =
3 5
4 6
3 7
4 7
>> result{3}
ans =
7 9
7 10
8 11

その他の回答 (1 件)

David Hill
David Hill 2020 年 4 月 11 日
Your example does not get the correct result.
Size_A =[10 10];
row_col = cell([],1);
for i=1:length(C)
[rows,cols] = ind2sub(Size_A,C{i}(:));
row_col{i} = [rows,cols];
end

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by