http://www.mathworks.com/matlabcentral/newsreader/view_thread/332299
MATLAB Central Newsreader  Fill cell array with matrix elements
Feed for thread: Fill cell array with matrix elements
enus
©19942015 by MathWorks, Inc.
webmaster@mathworks.com
MATLAB Central Newsreader
http://blogs.law.harvard.edu/tech/rss
60
MathWorks
http://www.mathworks.com/images/membrane_icon.gif

Sat, 26 Oct 2013 11:37:08 +0000
Fill cell array with matrix elements
http://www.mathworks.com/matlabcentral/newsreader/view_thread/332299#913199
Mohammed
Hello everybody,<br>
<br>
I have a cell array of zeros, and I wanted to fill it with elements of another matrix<br>
<br>
the ex is as follow;<br>
<br>
Temporary_Quad{1,1}=FieldStGP_Mixed_Quad(1:4,:); % size of Temporary_Quad is 4<br>
Temporary_Quad{2,1}=FieldStGP_Mixed_Quad(5:7,:); % size of Temporary_Quad is 3<br>
Temporary_Quad{3,1}=FieldStGP_Mixed_Quad(8:11,:); % size of Temporary_Quad is 4<br>
Temporary_Quad{4,1}=FieldStGP_Mixed_Quad(12:15,:); %size of Temporary_Quad is 4<br>
Temporary_Quad{5,1}=FieldStGP_Mixed_Quad(16:19,:);<br>
Temporary_Quad{6,1}=FieldStGP_Mixed_Quad(20:23,:);<br>
Temporary_Quad{7,1}=FieldStGP_Mixed_Quad(24:27,:);<br>
Temporary_Quad{8,1}=FieldStGP_Mixed_Quad(28:30,:);<br>
...<br>
...<br>
...<br>
<br>
how can i do that using a loop?<br>
<br>
I really need your help, because I am getting stuck right there!!<br>
<br>
Thanks alot!

Sat, 26 Oct 2013 21:18:10 +0000
Re: Fill cell array with matrix elements
http://www.mathworks.com/matlabcentral/newsreader/view_thread/332299#913213
Sadik
Hi,<br>
<br>
You can do it like this:<br>
<br>
for k = 1:8 <br>
Temporary_Quad{k,1}=FieldStGP_Mixed_Quad((k1)*4+1:k*4,:);<br>
end<br>
<br>
Best.

Sun, 27 Oct 2013 00:26:09 +0000
Re: Fill cell array with matrix elements
http://www.mathworks.com/matlabcentral/newsreader/view_thread/332299#913215
Mohammed
"Sadik " <sadik.hava@gmail.com> wrote in message <l4hbii$bbg$1@newscl01ah.mathworks.com>...<br>
> Hi,<br>
> <br>
> You can do it like this:<br>
> <br>
> for k = 1:8 <br>
> Temporary_Quad{k,1}=FieldStGP_Mixed_Quad((k1)*4+1:k*4,:);<br>
> end<br>
> <br>
> Best. <br>
<br>
Thank you a lot Sadik for your reply!<br>
This works only when Size(Temporary_Quad{1,1})=4, the next one, Temporary_Quad{2,1}, has the size 3, thats the main problem where I was stuck.<br>
<br>
I am sure there is a way to do it, you got the clue, I hope you could help me Sadik<br>
<br>
thank you a lot!!!

Sun, 27 Oct 2013 01:11:10 +0000
Re: Fill cell array with matrix elements
http://www.mathworks.com/matlabcentral/newsreader/view_thread/332299#913218
Sadik
Yes, you are right, I missed that. :D<br>
<br>
OK, so what you need to have is a separate vector which tells you what index a certain entry begins. So, this vector should be something like:<br>
<br>
beginningIndices = [1 5 8 12 16 20 24 28];<br>
<br>
Then you can do:<br>
<br>
for k = 1:length(beginningIndices)<br>
indStart = beginningIndices(k);<br>
<br>
if k < length(beginningIndices) <br>
indEnd = beginningIndices(k+1)1;<br>
else<br>
indEnd = size(FieldStGP_Mixed_Quad,1);<br>
end<br>
<br>
Temporary_Quad{k,1}=FieldStGP_Mixed_Quad(indStart:indEnd,:);<br>
<br>
end<br>
<br>
The only thing you need to modify in the above code is the beginningIndices vector to make sure that you reach to the last row of the FieldSt... matrix.<br>
<br>
Best.

Sun, 27 Oct 2013 08:22:13 +0000
Re: Fill cell array with matrix elements
http://www.mathworks.com/matlabcentral/newsreader/view_thread/332299#913228
Mohammed
"Sadik " <sadik.hava@gmail.com> wrote in message <l4hp7e$eed$1@newscl01ah.mathworks.com>...<br>
> Yes, you are right, I missed that. :D<br>
> <br>
> OK, so what you need to have is a separate vector which tells you what index a certain entry begins. So, this vector should be something like:<br>
> <br>
> beginningIndices = [1 5 8 12 16 20 24 28];<br>
> <br>
> Then you can do:<br>
> <br>
> for k = 1:length(beginningIndices)<br>
> indStart = beginningIndices(k);<br>
> <br>
> if k < length(beginningIndices) <br>
> indEnd = beginningIndices(k+1)1;<br>
> else<br>
> indEnd = size(FieldStGP_Mixed_Quad,1);<br>
> end<br>
> <br>
> Temporary_Quad{k,1}=FieldStGP_Mixed_Quad(indStart:indEnd,:);<br>
> <br>
> end<br>
> <br>
> The only thing you need to modify in the above code is the beginningIndices vector to make sure that you reach to the last row of the FieldSt... matrix.<br>
> <br>
> Best.<br>
<br>
Beautiful!!! it works perfectly, actually my cell array contains 56 cell "length(Temporary_Quad)=56", so had to do what you told me, to modify beginningIndices, but it took me a bit longer to calculate manually the biginning indices... so if it is a very big cell array what the best way to solve that?<br>
I am so sorry if I am bothering you asking so much thing my friend, I really appreciate you help... :) but, am i still supposed to modify beginningIndices every time or there is another manner?<br>
<br>
I am so sorry if I am bothering you asking so much thing my friend, I really appreciate you help... :)<br>
<br>
Thanks a lot!!!

Sun, 27 Oct 2013 18:26:15 +0000
Re: Fill cell array with matrix elements
http://www.mathworks.com/matlabcentral/newsreader/view_thread/332299#913247
Sadik
Yes, you have to do that because your indices do not seem to follow any rules that can be programmed.<br>
Best.

Sun, 27 Oct 2013 20:22:08 +0000
Re: Fill cell array with matrix elements
http://www.mathworks.com/matlabcentral/newsreader/view_thread/332299#913254
Mohammed
"Sadik " <sadik.hava@gmail.com> wrote in message <l4jls7$1ob$1@newscl01ah.mathworks.com>...<br>
> Yes, you have to do that because your indices do not seem to follow any rules that can be programmed.<br>
> Best.<br>
<br>
Thank you so much Sadik, that's exactly what I wanted to know, also I doubt it because this doesnt seem to follow any rule that can be programmed as you say :)<br>
<br>
Thanks alot for your help!!! I wish i could help in the future regarding programming...