How to populate a cell array with vector elements?

I have a cell array consisting of n-number of cells, each of different size. I also have a vector consisting of n-number of elements. I'd like to populate the cells with the corresponding elements from the vector.

1 件のコメント

Jan
Jan 2017 年 7 月 4 日
編集済み: Jan 2017 年 7 月 4 日
Is this a homework question? If it is: Sorry for posting the easy solution. Note that providing it as your solution would be a kind of cheating. If you ask a homework question, clarify this detail such that the answer can be formulated as hints and you have the chance to solve it by your own.

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

 採用された回答

Jan
Jan 2017 年 7 月 4 日
編集済み: Jan 2017 年 7 月 4 日

2 投票

What have you tried so far? This is easy with a loop:
C = {ones(1, 3); ones(1, 7); ones(1, 5)};
x = [3; 5, 7];
for k = 1:numel(C)
C{k} = C{k} * x(k);
end
There are some alternatives, which might be needed, if this is a homework question. Hint:
index = [1,1,1];
value = 8;
value(index)

その他の回答 (2 件)

Jan
Jan 2017 年 7 月 4 日

1 投票

I have a cell array consisting of n-number of cells
I assume this means:
n = 17;
C = cell(1, n);
But what does this mean:
each of different size.
? A short explanation would avoid to let the readers guess.
I also have a vector consisting of n-number of elements.
Perhaps this is:
x = rand(1, n)
and
I'd like to populate the cells with the corresponding elements from the vector.
might mean:
for k = 1:numel(C)
C{k} = x(k);
end
Or easier:
C = num2cell(x);
Does this help? If not explain what "each of different size" means.

1 件のコメント

Yerzhigit Bapin
Yerzhigit Bapin 2017 年 7 月 4 日
I have a cell array consisting of n-number of cells
Yes, in my case it is 5x1 cell.
each of different size.
I should have written each cell consists of vectors, which have different sizes.
I also have a vector consisting of n-number of elements.
Yes, in my case it is a column vector
Sorry for ambiguity.

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

Guillaume
Guillaume 2017 年 7 月 4 日

0 投票

Assuming you want to append the elements of your vector to vectors in the corresponding cells of your cell array:
result = arrayfun(@(c, el) [c{1}, el], yourcellarray, yourvector, 'UniformOutput', false);
The above assumes that the vectors in the cell arrays are row vectors. If they are column vectors, then it's [c{1}; el]

6 件のコメント

Yerzhigit Bapin
Yerzhigit Bapin 2017 年 7 月 4 日
I didn't get the desired result. The statement added an additional element from the vector to each cell. Does it matter if my vector is not a vector but a nx1 matrix?
Jan
Jan 2017 年 7 月 4 日
A nx1 matrix is a vector.
Guillaume
Guillaume 2017 年 7 月 4 日
編集済み: Guillaume 2017 年 7 月 4 日
I didn't get the desired result It's very unclear what is your desired result (and for that matter, the starting point). Provide an example of inputs and desired output.
The statement added an additional element from the vector to each cell. Yes, exactly as described in my sentence: "Assuming you want ...".
As Jan says, a nx1 matrix is a vector, a column vector to be precise. The definition of a vector is a matrix whose all dimensions but one have size 1.
Yerzhigit Bapin
Yerzhigit Bapin 2017 年 7 月 4 日
I have 5x1 cell "C" with ones vectors in each cell. Each vector has different size (1x50, 1x100 and so on). I have another vector "V" which is a 5x1 column vector (e.g. V = [2;3;4;5;6]. I need to have the values of vector "V" in place of ones (e.g. C{1,1} = [2 2 2 ...2]; C{2,1} = [3 3 3 ... 3] and so on), or to create another cell array, that would have dimensions similar to "C" containing vectors the same size that "C" has filled with values of V.
Thanks.
Jan
Jan 2017 年 7 月 4 日
@Yerzhigit Bapin: Do you see that an answer is much easier now after you have explained the wanted procedure clearly?
Guillaume
Guillaume 2017 年 7 月 4 日
OH, that certainly wasn't clear from the initial question.
A one-liner solution:
result = arrayfun(@(c, el) repmat(el, size(c{1})), yourcellarray, yourvector, 'UniformOutput', false);

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

カテゴリ

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

質問済み:

2017 年 7 月 4 日

コメント済み:

2017 年 7 月 4 日

Community Treasure Hunt

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

Start Hunting!

Translated by