how to create cell array using matrix data?

9 次查看(过去 30 天)
Hello everyone,
I have a data matrix (Data = 4x5x3) showing data in 3months
For example, data in january month
5 8 9 3 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
I want to create data cell with number of row(4) and column(5)
The final output will be
row column data
1 1 1x3
1 2 1x3
1 3 1x3
1 4 1x3
1 5 1x3
2 1 1x3
2 2 1x3
.
.
.
Thanks.
  4 个评论

请先登录,再进行评论。

回答(2 个)

Fangjun Jiang
Fangjun Jiang 2021-12-17
编辑:Fangjun Jiang 2021-12-17
I think you want to convert 4x5x3 matrix data into 4x5 cell, each cell is a 1x3 matrix data. There is a way to do it but I doublt it will bring you any benefit. Whatever you want to do with the cells, it can be achieved with the original matrix data.
If you want to convert it only for the purpose of interfacing with others, here is how to do it.
Data=rand(4,5,3)
NewData=num2cell(Data,3)
to verify
a1=Data(1,1,:)
b1=NewData{1,1}
To further achieve the format you want
Out=cellfun(@squeeze,NewData,'UniformOutput',false)
Out{1,1}
  1 个评论
Surendar Babu B
Surendar Babu B 2021-12-23
@Fangjun Jiang it works. But alongwith data I want column and row number also. the pupose of creating cell array is, I am doing dominance analysis which finds dominant variable among all independent variable.
The dominance function is ([Dom]=dominance(Y,X1))
where Y is dependent variable and X1 is independent variable (e.g., 5 independent variables). This dominance function requires data in vector format and I have all the data in matrix. So, If I create cell array alongwith row and column information then I can create matrix of Dom output. Am I doing correct?

请先登录,再进行评论。


Voss
Voss 2021-12-23
If you need to know the row and column in order to assign to the corresponding element in an output matrix, then there is no need to convert the data to a cell array and also keep track of rows and columns. Just call the function in loops and assign the result:
Data = randn(4,5,3);
display(Data);
Data =
Data(:,:,1) = -0.1028 1.1570 -0.4070 -0.0894 -0.4806 -0.6559 0.2893 2.1227 0.1894 0.1947 0.8489 -0.8347 0.6476 1.1886 -1.6488 -0.6444 -0.5148 -1.4753 -1.1452 -1.2327 Data(:,:,2) = 0.5436 -0.9115 0.1099 1.2186 -0.3155 -1.6735 2.9067 0.3722 1.0113 0.2082 0.2923 0.5541 0.5651 0.7585 0.0289 -1.1926 -0.1140 0.6932 0.0012 -1.1943 Data(:,:,3) = -0.3996 -0.7479 0.0911 -0.3349 -1.2307 0.0927 0.4828 0.5812 0.6075 -1.7760 1.0279 0.9808 -0.5775 -0.4965 1.1652 0.6841 0.3317 1.2718 -2.2124 1.2251
[nr,nc,~] = size(Data);
Dom = zeros(nr,nc);
for i = 1:nr
for j = 1:nc
Dom(i,j) = dominance(reshape(Data(i,j,:),[1 3]));
end
end
display(Dom);
Dom = 4×5
0.5436 -0.9115 0.1099 1.2186 -0.3155 -1.6735 2.9067 0.3722 1.0113 0.2082 0.2923 0.5541 0.5651 0.7585 0.0289 -1.1926 -0.1140 0.6932 0.0012 -1.1943
But if you really want the rows and columns, you can do this:
Data = randn(4,5,3);
[nr,nc,~] = size(Data);
rows = repelem(1:nr,nc);
cols = repmat(1:nc,[1 nr]);
display([rows(:) cols(:)]);
1 1 1 2 1 3 1 4 1 5 2 1 2 2 2 3 2 4 2 5 3 1 3 2 3 3 3 4 3 5 4 1 4 2 4 3 4 4 4 5
(dominance function defined here so the code will run. You would use your own.)
% use your own dominance function here
function out = dominance(in)
out = in(2);
end

类别

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