Filter löschen
Filter löschen

Loop in vector of cells

1 Ansicht (letzte 30 Tage)
Yossi
Yossi am 27 Feb. 2019
Kommentiert: Jan am 28 Feb. 2019
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 Kommentare
Yossi
Yossi am 28 Feb. 2019
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 am 28 Feb. 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.

Melden Sie sich an, um zu kommentieren.

Antworten (3)

Jan
Jan am 27 Feb. 2019
Bearbeitet: Jan am 27 Feb. 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 Kommentare
Yossi
Yossi am 28 Feb. 2019
Bearbeitet: Jan am 28 Feb. 2019
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 am 28 Feb. 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.

Melden Sie sich an, um zu kommentieren.


Adam Danz
Adam Danz am 27 Feb. 2019
Bearbeitet: Adam Danz am 27 Feb. 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

Yossi
Yossi am 27 Feb. 2019
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 Kommentare
Adam Danz
Adam Danz am 27 Feb. 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
Jan am 27 Feb. 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

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by