Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Fill cell array with matrix elements

Subject: Fill cell array with matrix elements

From: Mohammed

Date: 26 Oct, 2013 11:37:08

Message: 1 of 7

Hello everybody,

I have a cell array of zeros, and I wanted to fill it with elements of another matrix

the ex is as follow;

Temporary_Quad{1,1}=FieldStGP_Mixed_Quad(1:4,:); % size of Temporary_Quad is 4
Temporary_Quad{2,1}=FieldStGP_Mixed_Quad(5:7,:); % size of Temporary_Quad is 3
Temporary_Quad{3,1}=FieldStGP_Mixed_Quad(8:11,:); % size of Temporary_Quad is 4
Temporary_Quad{4,1}=FieldStGP_Mixed_Quad(12:15,:); %size of Temporary_Quad is 4
Temporary_Quad{5,1}=FieldStGP_Mixed_Quad(16:19,:);
Temporary_Quad{6,1}=FieldStGP_Mixed_Quad(20:23,:);
Temporary_Quad{7,1}=FieldStGP_Mixed_Quad(24:27,:);
Temporary_Quad{8,1}=FieldStGP_Mixed_Quad(28:30,:);
...
...
...

how can i do that using a loop?

I really need your help, because I am getting stuck right there!!

Thanks alot!

Subject: Fill cell array with matrix elements

From: Sadik

Date: 26 Oct, 2013 21:18:10

Message: 2 of 7

Hi,

You can do it like this:

for k = 1:8
    Temporary_Quad{k,1}=FieldStGP_Mixed_Quad((k-1)*4+1:k*4,:);
end

Best.

Subject: Fill cell array with matrix elements

From: Mohammed

Date: 27 Oct, 2013 00:26:09

Message: 3 of 7

"Sadik " <sadik.hava@gmail.com> wrote in message <l4hbii$bbg$1@newscl01ah.mathworks.com>...
> Hi,
>
> You can do it like this:
>
> for k = 1:8
> Temporary_Quad{k,1}=FieldStGP_Mixed_Quad((k-1)*4+1:k*4,:);
> end
>
> Best.

Thank you a lot Sadik for your reply!
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.

I am sure there is a way to do it, you got the clue, I hope you could help me Sadik

thank you a lot!!!

Subject: Fill cell array with matrix elements

From: Sadik

Date: 27 Oct, 2013 01:11:10

Message: 4 of 7

Yes, you are right, I missed that. :D

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:

beginningIndices = [1 5 8 12 16 20 24 28];

Then you can do:

for k = 1:length(beginningIndices)
    indStart = beginningIndices(k);
    
    if k < length(beginningIndices)
        indEnd = beginningIndices(k+1)-1;
    else
        indEnd = size(FieldStGP_Mixed_Quad,1);
    end
     
    Temporary_Quad{k,1}=FieldStGP_Mixed_Quad(indStart:indEnd,:);

end

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.

Best.

Subject: Fill cell array with matrix elements

From: Mohammed

Date: 27 Oct, 2013 08:22:13

Message: 5 of 7

"Sadik " <sadik.hava@gmail.com> wrote in message <l4hp7e$eed$1@newscl01ah.mathworks.com>...
> Yes, you are right, I missed that. :D
>
> 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:
>
> beginningIndices = [1 5 8 12 16 20 24 28];
>
> Then you can do:
>
> for k = 1:length(beginningIndices)
> indStart = beginningIndices(k);
>
> if k < length(beginningIndices)
> indEnd = beginningIndices(k+1)-1;
> else
> indEnd = size(FieldStGP_Mixed_Quad,1);
> end
>
> Temporary_Quad{k,1}=FieldStGP_Mixed_Quad(indStart:indEnd,:);
>
> end
>
> 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.
>
> Best.

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?
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?

I am so sorry if I am bothering you asking so much thing my friend, I really appreciate you help... :)

Thanks a lot!!!

Subject: Fill cell array with matrix elements

From: Sadik

Date: 27 Oct, 2013 18:26:15

Message: 6 of 7

Yes, you have to do that because your indices do not seem to follow any rules that can be programmed.
Best.

Subject: Fill cell array with matrix elements

From: Mohammed

Date: 27 Oct, 2013 20:22:08

Message: 7 of 7

"Sadik " <sadik.hava@gmail.com> wrote in message <l4jls7$1ob$1@newscl01ah.mathworks.com>...
> Yes, you have to do that because your indices do not seem to follow any rules that can be programmed.
> Best.

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 :)

Thanks alot for your help!!! I wish i could help in the future regarding programming...

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us