フィルターのクリア

Loop in vector of cells

1 回表示 (過去 30 日間)
Yossi
Yossi 2019 年 2 月 27 日
コメント済み: Jan 2019 年 2 月 28 日
Hello,
I have a Vector A, containing 1X2 cell. in each cell there are 1X252 data.
I want to creat a Vector B, containing 1X2 cells, which in each cell I'll take the value of the "n+1" from Vector A and have it in cell "n" vector B for each cell. How can I do that?
  24 件のコメント
Yossi
Yossi 2019 年 2 月 28 日
I meant a cell 1X2 i.e C={2,3} .
I want to multiply 2 in each cells at the first row: [1,2,3,4,5] and mutiply 3 with the second row [11,22,33,44,55].
Jan
Jan 2019 年 2 月 28 日
@Yossi: I have fixed your message by removing the trailing blank lines to increase the readability. I've explained this to you and it seems like you do not want to care about this advice.
If you want mult to be a cell, simply define it as cell and use the curly braces:
A = {[1,2,3,4,5], [11,22,33,44,55]};
B = cell(size(A));
mult = {2, 3};
% ^ ^
for iA = 1:numel(A)
B{iA} = A{iA}(2:end) * mult{iA};
% ^ ^
end
Again, a cellfun appraoch is still possible:
A = {[1,2,3,4,5], [11,22,33,44,55]};
mult = {2, 3};
B = cellfun(@(x,y) x(2:end) * y, A, mult, 'Uniformoutput', false);
I consider this question as solved, although you do not want to mark an accepted answer.

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

回答 (3 件)

Jan
Jan 2019 年 2 月 27 日
編集済み: Jan 2019 年 2 月 27 日
Maybe:
A = {[1,2,3,4,5], [11,22,33,44,55]};
B = cellfun(@(x) x(2:end), A, 'UniformOutput', false)
Or with a loop:
B = cell(size(A));
for iA = 1:numel(A)
a = A{iA};
B{iB} = a(2:end);
end
  5 件のコメント
Yossi
Yossi 2019 年 2 月 28 日
編集済み: Jan 2019 年 2 月 28 日
Thank you Jan,
I know its ineffective, but as I told, I need it for cell manipulation in order to use it for NN.
Jan
Jan 2019 年 2 月 28 日
@Yossi: So did this loop at least work? Then please mention this and mark a helpful answer as "accepted", such that the readers know that the problem is solved.
I cannot know what "use it for NN" means, but I really assume, that what ever it is, it will work with efficient methods also.

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


Adam Danz
Adam Danz 2019 年 2 月 27 日
編集済み: Adam Danz 2019 年 2 月 27 日
Based on several examples provided in comments under the question, it appears that the goal is to remove the first element of each vector stored in a cell array. If that is the case, loops are not necessary.
A={[1,2,3,4,5],[11,12,13,14,15]}; %desired outout: B={[2,3,4,5],[12,13,14,15]};
B = cellfun(@(x)x(2:end), A, 'UniformOutput', false);
celldisp(B)
B{1} =
2 3 4 5
B{2} =
12 13 14 15
You mentioned you'd like to "take each argument in each cell and place it in the second matrix". I interpret that as putting the elements of the cell array into a matrix.
% This puts the elements of B into a single row vector.
Bv = [B{:}];
%Bv =
% 2 3 4 5 12 13 14 15
% This puts the elements of B into a matrix but will only work if
% each element of B is the same size.
Bm = cell2mat(B');
%Bm =
% 2 3 4 5
% 12 13 14 15

Yossi
Yossi 2019 年 2 月 27 日
I was managed to solve it:
A={[1,2,3,4,5],[11,12,13,14,15]};
for iA=1:numel(A)
a=A{iA};
for iB=2:numel(a)
C(1,iB-1)=a(iB);
end
B(1,iA)={C};
end
  2 件のコメント
Adam Danz
Adam Danz 2019 年 2 月 27 日
That produces the same results as the solutions proposed here a while ago. If you don't need to use the for-loop, you should just do this in 1 line of code:
B = cellfun(@(x)x(2:end), A, 'UniformOutput', false);
Jan
Jan 2019 年 2 月 27 日
@Yossi: This will fail, if a vector is shorter than another one before, because then the former elements of C and not overwritten. As said already, your code does exactly the same as e.g. my answer, which has been given 7 hours ago:
a=A{iA};
for iB=2:numel(a)
C(1,iB-1)=a(iB);
end
% is the same as:
a = A{iA}
C = a(2:end);
% except that your loop does not crop a formerly existing C

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

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by