Loop in vector of cells
조회 수: 1 (최근 30일)
이전 댓글 표시
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
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
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
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
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
댓글 수: 0
Yossi
2019년 2월 27일
댓글 수: 2
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
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 Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!