Loop in vector of cells
Show older comments
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 Comments
Adam Danz
on 27 Feb 2019
Could you provide a short example? What would be the outcome of this?
A{1,1} = [1,2,3,4,5];
A{1,2} = [11,22,33,44,55];
Adam Danz
on 27 Feb 2019
So you just want to remove the first digit of each vector? What does this mean below?
I'll take the value of the "n+1" from Vector A and have it in cell "n" vector B for each cell.
Adam Danz
on 27 Feb 2019
I don't see that pattern in your example. In your example, the first number of each vector is removed.
If you're taking the 2nd number of one vector and putting it in position 1 of the next vector, wouldn't that look like this:
A{1,1} = [1,2,3,4,5];
A{1,2} = [2,22,33,44,55];
I'd like to help but I can't picture what you're describing.
What does it mean to "place [each argument] in the second matrix"? What does it mean to "extend" a matrix? What's "NN inside"?
Could you give a few examples? In the example you already gave, the only pattern I see is that you removed the first element of each vector.
Yossi
on 27 Feb 2019
Yossi
on 27 Feb 2019
Adam Danz
on 27 Feb 2019
Again, you're just removing the first element of each vector and that's a lot simpler than what you're describing.
Are all of these correct (below)?
% #1
A={[9 8 7 6 5 4 3],[4 5 6 7 8 9 10]};
B={[ 8 7 6 5 4 3],[ 5 6 7 8 9 10]};
% #2
A={[22 44 77 92],[999 777 555 222]};
B={[ 44 77 92],[ 777 555 222]};
% #3
A={[0 0],[8 8]};
B={[ 0],[ 8]};
Adam Danz
on 27 Feb 2019
Why do you need to use a loop? The answers below demonstrate how to produce "B" without a loop and these are more efficitent, cleaner, and easier to read than using a loop.
Jan
on 27 Feb 2019
@Yossi: I have posted a loop method several hours ago already. Simply use it: It removes the first element from each vector, exactly as in all of your examples.
madhan ravi
on 28 Feb 2019
@Yossi if you are too stubborn to not respond to any comments, next time don't expect help in this forum.
Jan
on 28 Feb 2019
@Yossi: This is your question and nobody but you suffers, if it is not solved. Take your time.
The argument of different time zones is not needed, because you do not have to justify yourself. But if you mention it, remember that you have even posted some comments after I've posted a solution and comments - without taking notice of the given hints and solutions.
The description of what you want to do still confuses me, but the given examples look like all you want to do is to remove the first element.
Yossi
on 28 Feb 2019
This is another question. You can implement this as:
A = {[1,2,3,4,5], [11,22,33,44,55]};
B = cell(size(A));
for iA = 1:numel(A)
a = A{iA};
B{iA} = a(2:end) * 2 + 3;
end
Or if you want to do this with a slower loop for any good reason (I do not know any):
B = cell(size(A));
for iA = 1:numel(A)
a = A{iA};
b = zeros(size(a)); % Important
for ib = 1:numel(a) - 1
b(ib) = a(ib + 1) * 2 + 3;
end
B{iA} = b;
end
Matlab is designed to operate on vectors and matrices efficiently. Such loops are rarely needed and the "vectorized" approach is nicer and faster. Matlab is not C, and it is worth to use its power.
Or with a cellfun call without a loop:
B = cellfun(@(x) x(2:end)*2+3, A, 'UniformOutput', false)
cellfun looks nice and compact, but usually the first method with the loop over the cell elements is faster.
These 3 methods are very similar to the already posted solutions of the original question. All I've added is "*2 + 3" . I assume you are able to implement this transfere by your own.
Adam Danz
on 28 Feb 2019
Just FYI, I've moved on to other questions in the forum and consider this one answered.
@Yossi: The thread is easier to read without trailing blank lines under a message. I'm removing them in all of your messages.
If the provided code works, please choose the answer, which helped, as "accepted".
{[2,3}] is no valid Matlab syntax, therefore I cannot guess, what it means. Use either curly braces {} or square brackets [].
The input data in your example are A = {[1,2,3,4,5], [11,22,33,44,55]} . So I cannot guess what "the first" and "the second" row means. Of course you can muliply each vector with another number:
A = {[1,2,3,4,5], [11,22,33,44,55]};
B = cell(size(A));
mult = [2, 3];
for iA = 1:numel(A)
a = A{iA};
B{iA} = a(2:end) * mult(iA) + 3; % Or whatever
end
If you do not have any experiences with programming, the forum is not the right place to learn the fundamental basics. Please read the "Getting started" chapters and Matlab's onramp (ask Google for these terms).
You are a member of this forum for over 2 years now. Actually I assume, that you know the basics already.
Yossi
on 28 Feb 2019
Jan
on 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.
Answers (3)
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 Comments
Yossi
on 27 Feb 2019
madhan ravi
on 27 Feb 2019
B = cellfun(@(x) x(2:end), A,'un',0)
Jan
on 27 Feb 2019
@Yossi: I cannot follow you. Maybe you mean:
B = cell(size(A));
for iA = 1:numel(A)
a = A{iA};
b = NaN(1, numel(a) - 1); % Pre-allocate
for ib = 1:numel(a) - 1
b(ib) = a(ib + 1);
end
B{iB} = b;
end
But this is very inefficient and unmatlabish.
Jan
on 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.
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
on 27 Feb 2019
0 votes
2 Comments
Adam Danz
on 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
on 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
Categories
Find more on Cell Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!