i want to replace cell data to index.

Hello,
I'm poor at English so write simple code..
cell data is a = {[1 2 3 4 ] [5 6] [ 7 8 9]} ; index = [2 1 3] ; -> 10 11 12
the result is a = {[1 10 3 4] [ 11 6] [7 8 12 ]}
cell data size are different.
i want to replace the cell data at the same time. How can i replace the data without using for loop. i don't know how to use cellfun function or another method.
for example,
at matrix a = [1 2 3 4 5] ; a([1 3]) = [100 200 ] ; the result is a=[100 2 200 4 5] ;
i want like this at cell.
thanks.

2 件のコメント

Jos (10584)
Jos (10584) 2014 年 1 月 20 日
Do you want to replace a value or insert it?
Seongjin
Seongjin 2014 年 1 月 21 日
I want to replace a value .

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

 採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2014 年 1 月 20 日

0 投票

I think this is the best way:
a = {[1 2 3 4 ] [5 6] [ 7 8 9]} ;
index = [2 1 3] ;
b=[10 11 12];
for k=1:numel(a)
a{k}(index(k))=b(k);
end
celldisp(a)

4 件のコメント

Seongjin
Seongjin 2014 年 1 月 20 日
Thank you for your answer. But, i don't want to use for loop.
Azzi Abdelmalek
Azzi Abdelmalek 2014 年 1 月 20 日
Without for loop, but not necessary faster
a = {[1 2 3 4 ] [5 6] [ 7 8 9]} ;
index = [2 1 3] ;
b=[10 11 12];
nn=1:numel(index);
out=arrayfun(@(ii) strrep(a{ii},a{ii}(index(ii)),b(ii)),nn,'un',0);
celldisp(out)
Seongjin
Seongjin 2014 年 1 月 20 日
編集済み: Seongjin 2014 年 1 月 20 日
Thank you so mush. Good luck.
i don't know. the run time of for-loop is faster than the runtime of function handle.
Azzi Abdelmalek
Azzi Abdelmalek 2014 年 1 月 20 日
Sometimes, the for loop do the job.

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

その他の回答 (3 件)

Jos (10584)
Jos (10584) 2014 年 1 月 20 日

0 投票

To insert values at a location in a cell of A:
A = {[1 2 3 4 ] [5 6] [ 7 8 9]} ;
index = [2 1 3] ;
B = [10 11 12] ;
fh = @(k) insertrows(A{k}.', B(k), index(k)-0.5).' ; % offset of 0.5 to insert _before_
OUT = arrayfun(fh,1:3,'un',false)
INSERTROWS can be found here:
http://www.mathworks.com/matlabcentral/fileexchange/9984-insertrows-v2-0-may-2008

1 件のコメント

Seongjin
Seongjin 2014 年 1 月 20 日
編集済み: Seongjin 2014 年 1 月 20 日
Thank you so mush. Good luck.
But,, I want to replace value,, not insert.
i try to solve the problem for your reference.

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

Andrei Bobrov
Andrei Bobrov 2014 年 1 月 20 日

0 投票

a = {[1 2 3 4 ] [5 6] [ 7 8 9]} ;
index = [2 1 3] ;
b = [10 11 12];
n = cellfun(@numel,a);
v = [a{:}];
ii = index +[0 cumsum(n(1:end-1))];
v(ii)=b;
out = mat2cell(v,1,n);
Jos (10584)
Jos (10584) 2014 年 1 月 22 日
編集済み: Jos (10584) 2014 年 1 月 23 日

0 投票

To replace values at a location in a cell of A:
Step 1: Create a function like this
function X = MyReplace(X,ii,V)
% MyReplace(X,ii,V) replaces X(ii) with V
X(ii) = V ;
Step 2: Use it like this:
% Example data
A = {[1 2 3 4 ] [5 6] [ 7 8 9]} ;
index = [2 1 3] ;
B = [10 11 12] ;
% Engine
fh = @(k) MyReplace(A{k},index(k),B(k)) ;
OUT = arrayfun(fh,1:3,'un',false)

カテゴリ

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

質問済み:

2014 年 1 月 20 日

編集済み:

2014 年 1 月 23 日

Community Treasure Hunt

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

Start Hunting!

Translated by