How can I convert a vector to a cell array? - matlab

I have a column vector I want to convert to a cell array such as:
A = rand(10,1);
B = cell(10,1);
for i=1:10
B{i} = A(i);
end
B =
[0.6221]
[0.3510]
[0.5132]
[0.4018]
[0.0760]
[0.2399]
[0.1233]
[0.1839]
[0.2400]
[0.4173]
How can I do this without an explicit for loop?
I tried:
B{:} = A(:)
and
[B{:}] = deal(A)
with no luck...
Also if possible, how can I do the same thing for a matrix, i.e. have each element in a cell by itself?

Use the function num2cell:
B = num2cell(A);
Works with matrices too.

Related

How can we use nchoosek() to get all the combinations of the rows of a matrix?

If we have a vector v of 1- 5 numbers we can use nchoosek(v,2) to get all the combinations having two elements. But this function does now allow us to get all the combinations of a matrix. I want to use it to get all the combinations of rows of a matrix.
Here's one way to do it:
function p = q47204269(inMat)
% Input handling:
if nargin == 0 || isempty(inMat)
inMat = magic(5);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
rowsCell = num2cell(inMat,2);
nRows = size(inMat,1);
p = cell(nRows,1);
for indR = 1:nRows
r = nchoosek(1:nRows,indR);
p{indR} = cell2mat(reshape(rowsCell(r.',:).',indR,1,[]));
end
See also:
The perms function, as it might come in handy in what you're doing.
This question.
with square matrix A
v = 1:size(A,1);
a = nchoosek(v,2);
B = zeros(2,size(A,1),length(a));
for i = 1:length(a)
B(:,:,i) = A(a(i,:)',:);
end
Each layer of array B is a 2 row matrix with the row combos from A
Not the most readable answer, but just for the sake of a one-liner :-)
A = randn(5,3); % example matrix
N = 2; % number of rows to pick each time
result = permute(reshape(A(nchoosek(1:size(A,1), N).', :), N, [], size(A,2)), [1 3 2]);
The result is a 3D array, such that each third-dim slice gives one of the a submatrices of A.

Multipl matrix in cell

I have a matrix A=[1,2,3] and a cell B={[1,2,3],[1,2,5],[1,2,6]}.
I would like the product of the matrix elements of a similar cell
that's mean A * B ={1*[1,2,3],2*[1,2,5],3*[1,2,6]};
Without loop in matlab
Here a few ways to multiply as you described:
% inputs
A = [1,2,3];
B = {[1,2,3],[1,2,5],[1,2,6]};
using for-loop:
C1 = cell(size(B));
for i=1:numel(C1)
C1{i} = A(i) * B{i};
end
using cellfun:
C2 = cellfun(#(a,b)a*b, num2cell(A), B, 'Uniform',false);
using bsxfun:
C3 = bsxfun(#times, A(:), cat(1,B{:}));
C3 = num2cell(C3,2)';
All results should be equal (output being a cell array):
assert(isequal(C1,C2,C3))
If I were to choose, I would stick with the for-loop. In this case it's likely faster and easiest to read.

writing a matrix in a loop

I have a series of arrays of equal length, and want to make a matrix for each data point of these, and perform some sort of operation such a multiplying the matrices.
a=ones(1,10);
b=3*ones(1,10);
c=zeros(1,10);
for i=1:10
A(i)=[a(i) a(i);
b(i) b(i)];
B(i)=[c(i) c(i)];
C(i)=B(i)*A(i);
end
Is this possible without using cells?
A = zeros(2,2,length(a));
B = zeros(length(a),:);
C = zeros(size(B));
for i=1:10
A(:,:,i)=[a(i) a(i);
b(i) b(i)];
B(i,:)=[c(i) c(i)];
C(i,:)=B(i,:)*A(:,:,i);
end
Note you can make A and B without loops:
aa = permute(A, [3,2,1]);
bb = permute(B, [3,2,1]);
A = [aa,aa;bb,bb];
B = [c.', c.'];

Use function eval( )+ function find()

%%%%%%%%%%%%% 2 - Old %%%%%%%%%%%%%%%%%%%%
I modified the code as suggested by achieve greater efficiency. I have new errors in the code, not being an expert of access to the cell array
for i = first:N_Files
mat{i} = load(files(i).name);
A{i} = mat{1,i};
x{i} = A{:,i};
ind{i} = find(x{i}>= -0.5 & x{i}<=0.5);
% New error
B{i} = A{ind{i,:},:}; **Index exceeds matrix dimensions.**
xx{i} = B(:,1);
end
%%%%%%%%%%%%% 1 - Old %%%%%%%%%%%%%%%%%%%%
I wrote a routine to access files placed in different folders.
Then build a cell array where are stored file data with the routine:
for i = first:N_Files
mat{i} = load(files(i).name); % 1x3 cell
eval(['A' num2str(i) '= mat{1,i} ;']) % A1,A2,A3 dim : 114336x6 double
end
A = {A1, A2, A3}; cell array 1x3
I have a problem of access to some vectors created on the evaluation of a matrix cell.
for i = first:N_Files
eval(['x' num2str(i) '= A{:,i} ;']) % create x1, x2, x3
% incorrect code
eval(['ind' num2str(i) '= find('x' num2str(i) >= -0.5 & 'x' num2str(i)
<=0.5) ;'])
end
% need this indexing solution
ind1 = find(x1>= -0.5 & x1<=0.5);
ind2 = find(x2>= -0.5 & x2<=0.5);
ind3 = find(x3>= -0.5 & x3<=0.5);
I necessity to have the eval function with the find function,
it's possible?
have a useful solution?
thanks
%%%%%%%%%%%%% Complete Code %%%%%%
This is the code that I had to get.
for i = first:N_Files
A{i} = load(files(i).name); % 1x3 cell
x = A{:,1};
ind{i} = find(x(:,1)>= -0.5 & x(:,1)<=0.5); % This looks cleaner
B{i} = A{i}(ind{i},:); % Correct,first access element i in A then filter.
xx{i} = B{i}(:,1); %1x3 cell **what i really wanted**
yy{i} = B{i}(:,2);
zz{i} = B{i}(:,3);
u{i} = B{i}(:,4); % vettore velocità
v{i} = B{i}(:,5);
w{i} = B{i}(:,6);
c1{i} = [-0.5:0.01:0.5];
c2{i} = [0:0.01:2] ;
[X{i},Z{i}] = meshgrid(c1{i},c2{i});
U{i} = griddata(xx{i},zz{i},u{i},X{i},Z{i});
U{i}(isnan(U{i}))=0; % interpolazione ai bordi
W{i} = griddata(xx{i},zz{i},w{i},X{i},Z{i});
W{i}(isnan(W{i}))=0;
figure
pcolor(X{i},Z{i},(U{i}.^2+W{i}.^2).^0.5);
shading(gca,'interp')
title('Velocità')
colorbar;
axis square
hh = streamslice(X{i},Z{i},U{i},W{i});
set(hh,'color','k');
end
Ok I will try to help you as good as I can. First, if you have a cell that have the dimension 1xN, you do only need one index: A{i} = mat{i}. However, you then see that A==mat, which means that both are not needed. On the next row, you do x{i} = A{:,i} which seems to be the same as you do on the former row => x==mat. This means that we can remove two rows. Then I guess that your goal is to find the indices which have an absolute value smaller than 0.5 for each file and store each in a cell, right? Then to the error: By doing A{ind{i,:},:} you are actually sub-referencing A itself and not each element of A. The size of A is 1*nFiles. What you are trying to do is indeed this: B{i} = A{i}(ind{i}).
So if this is not what you want please comment. Otherwise, remove redundant variables. And make sure that you do not mix up cell with matrix. You use a cell as a container for arrays: a{n} refers to the array in cell element n and a{n}(m) refers to matrix element m in the array located in cell element n. Good luck!
for i = first:N_Files
mat{i} = load(files(i).name);
%A{i} = mat{1,i}; % not needed mat==x
%x{i} = A{:,i}; % not needed x==A
x = mat; % Fix this later, I do not want to change any variable names.
% ind{i} = find(x{i}>= -0.5 & x{i}<=0.5);
ind{i} = find(abs(x{i})<=0.5); % This looks cleaner
%B{i} = A{ind{i,:},:}; **Index exceeds matrix dimensions.**
B{i} = A{i}(ind{i}); % Correct,first access element i in A then filter.
xx{i} = B(:,1); %I do not know what you tries to do but probably
%xx{i}=B{i} => x==B so one variable is redundant.
end

Matlab How to perform substraction for multiple cell arrays?

The code below is only for 2 cell arrays, named B and C
A=cellfun(#minus, B, C, 'UniformOutput', false)
I want to perform a loop to be able to perform substraction for all my cell arrays.
Example of B{i} and C{i} are below:
B{1}=[0.435]
B{2}=[0.333] [0.532]
B{3}=[0.021] [0.432] [0.312] //command window output
C{1}=[0.211]
C{2}=[0.243] [0.116]
C{3}=[0.553] [0.212] [0.375] //command window output
B{1}-C{1}
B{2}-C{2}
B{3}-C{3}
I tried to include {i} behind A , B and C to become something like:
A{i}=cellfun(#minus, B{i}, C{i}, 'UniformOutput' , false)
However, it seems like it's not working. Is there any solution for this? Thanks
EDIT:
You have unnecessary nested cell-arrays, i.e B is a cell-array of cell-arrays, and B{i} is a cell-array of numbers.
If you want to keep that format, here is one way to compute the result using cellfun (A will also be a cell-array of cell-arrays of numbers):
% exiting data
B = cell(3,1);
B{1} = {0.435};
B{2} = {0.333, 0.532};
B{3} = {0.021, 0.432, 0.312};
C = cell(3,1);
C{1} = {0.211};
C{2} = {0.243, 0.116};
C{3} = {0.553, 0.212, 0.375};
A = cellfun(#(b,c)cellfun(#minus, b, c, 'Uniform',false), B, C, 'Uniform',false);
Otherwise I suggest you cut down on the level of nesting, and use this instead:
% note the difference between square-brackets and curly-brackets
B = cell(3,1);
B{1} = [0.435];
B{2} = [0.333, 0.532];
B{3} = [0.021, 0.432, 0.312];
C = cell(3,1);
C{1} = [0.211];
C{2} = [0.243, 0.116];
C{3} = [0.553, 0.212, 0.375];
Now you can compute the result using a single cellfun (no nesting):
A = cellfun(#minus, B, C, 'Uniform',false)