regarding storage issues for mixed-type value matrix

2 次查看(过去 30 天)
There has a loop in my program, and during each iteration an ID will be generated. I want to store these IDs into a two dimensional array, i.e., A. The first column of A stores the iteration number, i.e., A(1,1) = 1 and A(2,1) = 2. The second column of A stores the ID generated during each iteration, i.e., A(1,2) stores the ID generated during the first iteration. The tricky part is that these IDs can be either a numerical value or a string. For instance, A(1,2) = 12345; A(2,2) = abcde. Which kind of data structure should I use to store this mixed-value matrix?

采纳的回答

Andrei Bobrov
Andrei Bobrov 2011-11-23
% cell array
A = {1 12345;2 'abcde'}
% structure array
A = struct('iter',{1 2},'ID',{12345,'abcde'})
A.ID
A.iter
e.g.
A = cell(2);
for j1 = 1:2
A{j1,1} = j1;
if j1 == 1
A{j1,2} = 12345; % or other
else
A{j1,2} = 'abcde';
end
end
OR
A = struct('iter',[],'ID',[]);
for j1 = 1:2
A(j1).iter = j1;
if j1 == 1
A(j1).ID = 12345; % or other
else
A(j1).ID = 'abcde';
end
end

更多回答(1 个)

Image Analyst
Image Analyst 2011-11-23
I'd use an array of structures. I think it would be simplest. However you can use a cell array if you want, though figuring out when to use parentheses and when to use braces can be tricky.

类别

Help CenterFile Exchange 中查找有关 Cell Arrays 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by