MATLAB Answers

How to pass a Matrix of cell array as a input argument to a function

12 views (last 30 days)
Hey all,
I already asked this question in different way. But now I came up with my code instead of just explanation.
This is my user input matrix file:
function [Lambda, A, B, D] = Matrix_surendra13
n =7 ;
% Conductivity Matrix size preallocation
Lambda = ones(1,n);
% Area Matrix size preallocation
A = ones(n,n);
% Diameter matrix Size definition
D = ones (1,n);
%Width matrix Size definition
B = ones(1,n);
Lambda = [205 50.2 0.025893 79.5 0.180 7720 0.025893];
D = [ 55 55 0 55 0 55 0];
%Here you can see in cell array A , A{1,2} and A{2,1} are same. So I need to pass whole matrix as input arguments to the function in the slover.
A = { 0 [ 2375.80 4749.50 1457.25] 0 0 0 0 2375.80 ;
[ 2375.80 4749.50 1457.25] 0 2375.80 4749.50 0 0 1835.80;
0 2375.80 0 0 0 0 0;
0 4749.50 0 0 2375.80 4749.50 1835.80;
0 0 0 2375.80 0 0 0;
0 0 0 4749.50 0 0 2375.80 ;
2375.80 1835.80 0 1835.80 0 2375.80 0};
B = [ 25 30 0 20 0 35 0];
end
This is my solver:
function SampleModel13_Stat(Measuring_Data)
[Lambda, A, B, D] = Matrix_surendra13;
n = size(A,1);
m = size(A,2);
LA = zeros(n,m);
for i=1:n
for j=1:n
% for ii = 1: after colon, size of the matrix inside cell array
% for jj = 1: after colon size, of the matrix inside cell array, but I am not sure whether it is %correct or not to pass the matrix elements one by one at the same time.
%How to find the size of the matrix inside the cell array.
LA(i,j) = Leitwert_L_2_3(Lambda(i),Lambda(j), A{i,j}A(ii,jj), A{i,j}A(ii,jj), A{i,j}A(ii,jj), B(i),B(j));
%end
%end
LA(i,j)= Leitwert_L(Lambda(i), Lambda(j), A(i,j), B(i), B(j)); % this is fine for the rest of the things
end
end
disp(LA);
end
I have two questions.
First one is, How to find the size of the matrix inside the cell array. may be using length(A{1,2}, I will get only the length of row elements. but not the size of the matrix or using numel.
Second one is How to pass all the arguments of the matrix of the cell array A{1,2}, to a function in the slover.
Like below
A{1,2}A(1,1), A{1,2}A(1,2), A{1,2}A(1,3) %first element of the matrix is for A{1,2}A(1,1) and same for rest of the things.
A{2,1}A(1,1), A{2,1}A(1,2), A{2,1}A(1,3)
I don't know whether it is possible using for loop or not, but I am trying it still.
I hope that explantion is enough, if you still need an explantion, I am ready to give.
Any suggestions and answers would be much appreciated
Thanks in Advance

  2 Comments

Bob Nbob
Bob Nbob on 24 Jun 2019
I'm not sure what you mean with this designation: A{1,2}A(1,1). Are you just trying to access the first element of the A{1,2}? If so, that would be written out this way, A{1,2}(1,1). (You don't need the second 'A.'
Getting the size of an array can be accomplished with size. [r,c] = size(A{1,2})
'Second one is How to pass all the arguments of the matrix of the cell array A{1,2}, to a function in the slover.'
When you say, 'pass the arguments,' I'm assuming that you mean the elements of a matrix. Do you want them passed individually, or the entire cell at the same time? The for loops that you have set up will accomplish this, though they may take some time. You can try using cellfun, but at its heart it is still a for loop.
surendra kumar Aralapura mariyappa
@Bob Nbob,
' When you say, 'pass the arguments,' I'm assuming that you mean the elements of a matrix. Do you want them passed individually, or the entire cell at the same time? '
I need to pass the elements of the matrix individually at the same time?
How can I accomplish this using for loop?
Can you just give me an hint to make it ?
Thanks in advance.

Sign in to comment.

Accepted Answer

Bob Nbob
Bob Nbob on 24 Jun 2019
In order to pass elements 'individually at the same time' you need to use something like parfor, which requires the parallel computing tool box, which is not something that I have any knowledge of. Is there a specific reason you need the individual elements at the same time, and not just sequentially?
If you are looking at them sequentially, then your four for loops will work fine, just use the size command I mentioned earlier.
for i = 1:size(A,1) % Each row of cells
for j = 1:size(A,2) % Each column of cells
for k = 1:size(A{i,j},1) % Row of cell contents
for m = 1:size(A{i,j},2) % Column of cell contents
tmp = A{i,j}(k,m);
end
end
end
end

More Answers (0)

Community Treasure Hunt

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

Start Hunting!