how to assign value to a specific position in cell

13 ビュー (過去 30 日間)
SM
SM 2020 年 7 月 13 日
コメント済み: Walter Roberson 2020 年 7 月 17 日
A={[10,11,10,7],[7,8,7,6],[11,10,9,9],[7,5,11,5];[9,8,6,11],[10,6,9,7],[6,12,8,9],[7,10,12,9];[10,7,12,6],[8,11,7,5],[8,5,9,9],[10,10,10,10]};
B= [1,1,0,1;0,1,1,1;1,0,1,1;0,1,1,0];
[a,b]=find(~B);
for i=1:numel(a)
A{a(i),b(i)}=[100 100 100 100];
end
Is there any other ways instead of using for loop?

採用された回答

Walter Roberson
Walter Roberson 2020 年 7 月 13 日
if size(B,1) > size(A,1)
A(size(B,1),1) = {}; %extend A if needed
end
if size(B,2) > size(A,2)
A(1,size(B,2)) = {}; %extend A if needed
end
A(~B) = {[100 100 100 100]}; %replaces the find and the loop
  2 件のコメント
SM
SM 2020 年 7 月 13 日
編集済み: SM 2020 年 7 月 13 日
Excellent solution. Thank you!
Is there any other ways instead of using for loop for this one?
A={[10,11,10,7],[7,8,7,6],[11,10,9,9],[7,5,11,5];[9,8,6,11],[10,6,9,7],[6,12,8,9],[7,10,12,9];[10,7,12,6],[8,11,7,5],[8,5,9,9],[10,10,10,10]};
C=[1 1 0 1;0 1 1 1;1 0 1 1; 0 1 1 0];
[a,b]=find(~C);
for i=1:numel(a)
for j=1:size(A,1)
A{j,a(i)}(b(i))=100;
end
end
Thanks in advance!
Walter Roberson
Walter Roberson 2020 年 7 月 17 日
You might be able to hide loops behind some cellfun() or arrayfun(), and abusing subsasgn() to do assignments using function call syntax...but otherwise, NO.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by