Loop in vector of cells
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Jan
le 28 Fév 2019
@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.
Réponses (3)
Jan
le 27 Fév 2019
Modifié(e) : Jan
le 27 Fév 2019
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 commentaires
Jan
le 28 Fév 2019
@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
le 27 Fév 2019
Modifié(e) : Adam Danz
le 27 Fév 2019
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 commentaires
Yossi
le 27 Fév 2019
2 commentaires
Adam Danz
le 27 Fév 2019
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
le 27 Fév 2019
@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
Voir également
Catégories
En savoir plus sur Data Type Conversion dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!