brucezepplin
brucezepplin

Reputation: 9782

create a paired value cell array from existing vectors

and have two vectors - a and b. a is of class double, and b is of class cell. I want to create a 2 x length(a) cell array that pairs the 1st value of a with the second value of b and so on....

I have so far

for i=1:length(a)
    for j=1:length(ab
    c{j,i} = {a(j),cell2mat(b(i))};
    end
end

where each output of my new structure is something like this is c{1,1}:

c{1,1} = 

    [-0.1065]    [1x499 char]

where I cannot seem to access the second element.My question is is there a way to access that second element for each row in the cell array, or have I done this wrong?

Thanks very much.

Upvotes: 1

Views: 133

Answers (2)

bla
bla

Reputation: 26069

No need for a loop. This is how you can do it assuming both your cell and numeric vectors are columns:

a=[1:4]';
b={'a';'b';'c';'d'};
c=[num2cell(a),b]    % combine a to b in a cell array

Upvotes: 2

Daniel
Daniel

Reputation: 36720

You are creating a very strange datastructure, cell of cell of arrays.

x=c{1,1}
first=x{1}
second=x{2}

Upvotes: 1

Related Questions