Hi everyone,
I have created a cell array of dimensions 1x10, named A. Each element contains a 100x5 matrix. Hence, I got 10 matrices 100x5. However, I want to put every matrix of the cell array into a loop. If Bis a 100x5 matrix, the loop should look like:
for t=1:100;
j=1:5;
x=B(t,j)-A(t,j)
end;
end;
At the end x should deliver a 1x10 cell array that will contain 10 elements of matrices 100x5.
I would appreciate any help. Thank you in advance!

답변 (1개)

Mostafa
Mostafa 2016년 11월 9일
편집: Mostafa 2016년 11월 9일

0 개 추천

% B is already defined, a matrix of doubles
% A is already defined, a cell array of matrices
% B and the matrices in A have the same size
x = cellfun(@(X) B - X, A, 'UniformOutput', 0);

댓글 수: 6

gsourop
gsourop 2016년 11월 9일
Hi Mostafa,
Thanks a lot for the answer, it was very helpful. If I want to change the specifications a little bit so that the former loop is transformed into for t=1:100; j=1:5; x=c*inv((B(t,j)-A(t,j))*((B(t,j)-A(t,j))')*(A(t,j)-C(t,1)*ones(1,5)); end; end; where C is a 100x1 vector and c is a constant.x should deliver a 1x10 cell array of elements 100x5, as before. How does the command cellfun work on this case? I appreciate it. Thanks in advance!
Mostafa
Mostafa 2016년 11월 9일
You missed a bracket somewhere so I don't know do what you want this to evaluate to
x=c*inv((B(t,j)-A(t,j))*((B(t,j)-A(t,j))')*(A(t,j)-C(t,1)*ones(1,5));
In any case, what cellfun does is that it loops on a cell array and applies the same function to all the elements in it. In your case, each element is a 100x5 matrix. It can be also written as this:
for i = 1:length(A)
x{i} = B - A{i};
end
%Since A is a cell and we need to address the data inside, we used A{i}
Matlab can evaluate a summation or subtraction of two matrices when they have the same size, and can do multiplication when the inner dimensions agree (100x5 * 5x100). What you can also do, is to multiply each element in the matrix to the corresponding one in the next matrix using the .* notation.
%For example:
x = cellfun(@(X) B.*X, A, 'UniformOutput', 0);
%Let's say you want to evaluate this:
C2 = = C*ones(1,5);
x = cellfun(@(X) c*inv((B-X)*((B-X)'))*(X-C2), A, 'UniformOutput', 0);
As you can see, using cellfun doesn't change the notation that much. You need to identify the cell array (X) and write it in the equation as X, then pass it as the second argument to the cellfun.
%So basically
cellfun(@(Variable) Variable*2, CellArray, 'UniformOutput', 0);
Note, for accuracy, the exact equivalent loop of the cellfun statement would be:
x = cell(size(A)); %output will be exactly the same shape as input
for i = 1 : numel(A) %certainly not length
x{i} = B - A{i};
end
Mostafa
Mostafa 2016년 11월 10일
Since we are dealing with a 1-D array, numel and length will evaluate to the same value. I tend to use length with 1-D arrays and numel with 2-D arrays to improve code readability.
Also, pre-allocation is a good idea since it saves sufficient processor time, so consider @Guillaume's advice whenever you want to make a for loop.
Guillaume
Guillaume 2016년 11월 10일
While preallocation is indeed advisable, my main reason for creating x beforehand was actually to demonstrate that cellfun creates an output the same shape as the input. Without that preallocation, the output is a row vector.
The problem with length is that you open yourself to bugs if in the future the input changes from vector to multidimensional. The above is a perfect example: if the input is a matrix instead of a vector, your loop will only process the first X elements of the ND cell array, where X is the size of the largest dimension.
My version with numel works with any shape and any number of dimensions.
For that reason, I never use length. If I want to operate over all the elements of the inputs (regardless of shape), I use numel. If I want to operate over a particular dimension, I use size with the dimension specified.
Mostafa
Mostafa 2016년 11월 13일
I'm not arguing with your point, I'm saying that it's a personal preference in case of 1-D arrays (which was the case in the fore mentioned example). In my codes, whenever I see length, I immediately understand that I'm dealing with an array - not a matrix or an input of unspecified size.
I totally agree that the general form is to use numel, or even better: cellfun

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Data Type Conversion에 대해 자세히 알아보기

질문:

2016년 11월 9일

댓글:

2016년 11월 13일

Community Treasure Hunt

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

Start Hunting!

Translated by