How to insert the missing number into the cell array?

Suppose that I have this
I=[3 5 6 8 10 11]
A={[3];[6;8];[];[11]}
How to find the missing number of I in A and insert that number into any position of A? for intance like:
A={[3];[6;8];[5];[10;11]}

4 件のコメント

KSSV
KSSV 2019 年 5 月 14 日
YOu have I in hand always?
Hang Vu
Hang Vu 2019 年 5 月 14 日
yes I have I in hand, it would be changed depend on my input.
Akira Agata
Akira Agata 2019 年 5 月 14 日
編集済み: Akira Agata 2019 年 5 月 14 日
One quick question:
How did you define the position of '10' in your cell array A?
Hang Vu
Hang Vu 2019 年 5 月 14 日
It will be random sir. That is just my example, I want to randomly insert the missing numbers into A, not only the empty cell

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

 採用された回答

KSSV
KSSV 2019 年 5 月 14 日

0 投票

I=[3 5 6 8 10 11] ;
A={[3];[6;8];[];[11]} ;
% get empty cell
idx = cellfun(@isempty,A) ;
A{idx} = setdiff(I,cell2mat(A))

11 件のコメント

Hang Vu
Hang Vu 2019 年 5 月 14 日
Thank you, but I want to insert in any position not only the empty cell. and it's should display as 1 column. Yours is 1x2
A =
4×1 cell array
{[ 3]}
{2×1 double}
{1×2 double}
{[ 11]}
KSSV
KSSV 2019 年 5 月 14 日
I inserted in the empty cell, the elements left in I. YOu can use the following:
A{1}(end+1) = 5 ; % this will insert in first cell in second position
Hang Vu
Hang Vu 2019 年 5 月 14 日
Thank you for your time! But with that way it is just manually input
KSSV
KSSV 2019 年 5 月 14 日
Then how you want?
Hang Vu
Hang Vu 2019 年 5 月 14 日
Because I have here are 2 missing numbers, and I don't know these two, How can I use
A{1}(end+1) = 5 ;
KSSV
KSSV 2019 年 5 月 14 日
I have showed you how to get missing numbers already. :
miss = setdiff(I,cell2mat(A))
Hang Vu
Hang Vu 2019 年 5 月 14 日
this setdiff found 2 numbers but the A{1}(end+1) is only 1 position, so it doesn't work. And do you have any way to ramdom insert instead of typing the position?
KSSV
KSSV 2019 年 5 月 14 日
idx = randsample(length(A),1)
Hang Vu
Hang Vu 2019 年 5 月 14 日
Thank you, but Do you get my point? I already tried this
I=[3 5 6 8 10 11] ;
A={[3];[6; 8];[];[11]} ;
% get empty cell
idx = randi(size(A,1))
A{idx}= setdiff(I,cell2mat(A))
Could you please make this with your way?
A={[3];[6;8];[5];[10;11]}
KSSV
KSSV 2019 年 5 月 14 日
I=[3 5 6 8 10 11] ;
A={[3];[6;8];[];[11]} ;
% get empty cell
idx = cellfun(@isempty,A) ;
missing = setdiff(I,cell2mat(A)) ;
A{idx} = missing(1)
Hang Vu
Hang Vu 2019 年 5 月 14 日
編集済み: Hang Vu 2019 年 5 月 14 日
Thank you so much! I modified a bit and it worked.

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

その他の回答 (0 件)

カテゴリ

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

質問済み:

2019 年 5 月 14 日

編集済み:

2019 年 5 月 14 日

Community Treasure Hunt

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

Start Hunting!

Translated by