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:
add a few matrix

Subject: add a few matrix

From: wahyoe Unggul

Date: 1 Jan, 2011 15:01:06

Message: 1 of 12

hai
how do I add a few matrix
jl1 = [2 4 5]
jl2 = [3 4 6]
jl3 = [3 5 6]
jli = [a b c]

Subject: add a few matrix

From: Oleg Komarov

Date: 1 Jan, 2011 16:02:04

Message: 2 of 12

"wahyoe Unggul" <wahyoe_slipnot@yahoo.co.id> wrote in message <ifnfjh$k8d$1@fred.mathworks.com>...
> hai
> how do I add a few matrix
> jl1 = [2 4 5]
> jl2 = [3 4 6]
> jl3 = [3 5 6]
> jli = [a b c]

I assume that adding is the mathematical operation, then:
jl1 + jl2 + ...

If you want to add them dynamically I suggest to generate those matrices as fields of a structure or to store them in a cell array.

J.jl1 = jl1;
J.jl2 = jl2;
...

Out = zeros(size(J.jl1));
fnames = fieldnames(J);
for f = 1:numel(fnames)
     Out = Out + J.(fnames{f});
end

Oleg

Subject: add a few matrix

From: wahyoe Unggul

Date: 1 Jan, 2011 16:25:05

Message: 3 of 12

"Oleg Komarov" <oleg.komarovRemove.this@hotmail.it> wrote in message <ifnj5s$7sb$1@fred.mathworks.com>...
> "wahyoe Unggul" <wahyoe_slipnot@yahoo.co.id> wrote in message <ifnfjh$k8d$1@fred.mathworks.com>...
> > hai
> > how do I add a few matrix
> > jl1 = [2 4 5]
> > jl2 = [3 4 6]
> > jl3 = [3 5 6]
> > jli = [a b c]
>
> I assume that adding is the mathematical operation, then:
> jl1 + jl2 + ...
>
> If you want to add them dynamically I suggest to generate those matrices as fields of a structure or to store them in a cell array.
>
> J.jl1 = jl1;
> J.jl2 = jl2;
> ...
>
> Out = zeros(size(J.jl1));
> fnames = fieldnames(J);
> for f = 1:numel(fnames)
> Out = Out + J.(fnames{f});
> end
>
> Oleg

hai Oleg
I still do not understand, what if you give an example or instance :
JL = jl1+jl2+jl3+jli

Subject: add a few matrix

From: wahyoe Unggul

Date: 1 Jan, 2011 16:29:05

Message: 4 of 12

"Oleg Komarov" <oleg.komarovRemove.this@hotmail.it> wrote in message <ifnj5s$7sb$1@fred.mathworks.com>...
> "wahyoe Unggul" <wahyoe_slipnot@yahoo.co.id> wrote in message <ifnfjh$k8d$1@fred.mathworks.com>...
> > hai
> > how do I add a few matrix
> > jl1 = [2 4 5]
> > jl2 = [3 4 6]
> > jl3 = [3 5 6]
> > jli = [a b c]
>
> I assume that adding is the mathematical operation, then:
> jl1 + jl2 + ...
>
> If you want to add them dynamically I suggest to generate those matrices as fields of a structure or to store them in a cell array.
>
> J.jl1 = jl1;
> J.jl2 = jl2;
> ...
>
> Out = zeros(size(J.jl1));
> fnames = fieldnames(J);
> for f = 1:numel(fnames)
> Out = Out + J.(fnames{f});
> end
>
> Oleg

hai oleg
I still do not understand, what if you give an example or instance
JL=jl1+jl2+jl3+jli

Subject: add a few matrix

From: ImageAnalyst

Date: 1 Jan, 2011 17:00:52

Message: 5 of 12

On Jan 1, 11:29 am, "wahyoe Unggul" <wahyoe_slip...@yahoo.co.id>
wrote:
> I still do not understand, what if you give an example or instance
> JL=jl1+jl2+jl3+jli
-----------------------------
Yes, that will work (as long as a, b, and c are numbers), as you could
have easily found out had you tried it.

Subject: add a few matrix

From: wahyoe Unggul

Date: 1 Jan, 2011 21:34:04

Message: 6 of 12

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <72f453be-57d1-49b0-adcf-2c6d765f6e20@v17g2000yqv.googlegroups.com>...
> On Jan 1, 11:29 am, "wahyoe Unggul" <wahyoe_slip...@yahoo.co.id>
> wrote:
> > I still do not understand, what if you give an example or instance
> > JL=jl1+jl2+jl3+jli
> -----------------------------
> Yes, that will work (as long as a, b, and c are numbers), as you could
> have easily found out had you tried it.

hai imageanalyst
yes, a, b, and c are numbers
Can you give an example if
JL = jl1 + jl2 + jl3 + jli

Subject: add a few matrix

From: ImageAnalyst

Date: 1 Jan, 2011 21:51:55

Message: 7 of 12

I don't understand why you aren't even trying it in MATLAB. Just copy
your code from the newsreader into MATLAB and hit the run button.
It's as simple as that. Why are you not trying it??? Here's *your*
example:

a=1
b=3
c=8

% wahyoe's code below:
jl1 = [2 4 5]
jl2 = [3 4 6]
jl3 = [3 5 6]
jli = [a b c]
JL = jl1+jl2+jl3+jli

Results:

a =
     1
b =
     3
c =
     8
jl1 =
     2 4 5
jl2 =
     3 4 6
jl3 =
     3 5 6
jli =
     1 3 8
JL =
     9 16 25

Subject: add a few matrix

From: wahyoe Unggul

Date: 1 Jan, 2011 22:32:05

Message: 8 of 12

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <38323b18-1aef-46ed-adfa-2aefebaf4cd5@o4g2000yqd.googlegroups.com>...
> I don't understand why you aren't even trying it in MATLAB. Just copy
> your code from the newsreader into MATLAB and hit the run button.
> It's as simple as that. Why are you not trying it??? Here's *your*
> example:
>
>

> jl1 = [2 4 5]
> jl2 = [3 4 6]
> jl3 = [3 5 6]
> jli = [a b c]

sorry, that's not what I mean
a, b and c is the number
JL = jl1 + jl2 + jl3 +.......+ jli

Subject: add a few matrix

From: Oleg Komarov

Date: 1 Jan, 2011 23:13:04

Message: 9 of 12

"wahyoe Unggul" <wahyoe_slipnot@yahoo.co.id> wrote in message <ifoa15$keq$1@fred.mathworks.com>...
> ImageAnalyst <imageanalyst@mailinator.com> wrote in message <38323b18-1aef-46ed-adfa-2aefebaf4cd5@o4g2000yqd.googlegroups.com>...
> > I don't understand why you aren't even trying it in MATLAB. Just copy
> > your code from the newsreader into MATLAB and hit the run button.
> > It's as simple as that. Why are you not trying it??? Here's *your*
> > example:
> >
> >
>
> > jl1 = [2 4 5]
> > jl2 = [3 4 6]
> > jl3 = [3 5 6]
> > jli = [a b c]
>
> sorry, that's not what I mean
> a, b and c is the number
> JL = jl1 + jl2 + jl3 +.......+ jli

Please read the first 4 chapter of the getting started guide, then state your query.

If you want to find the sum of the from term 1 to the i-th term then as I said store differently your arrays:
jl1 = [2 4 5];
jl2 = [3 4 6];
jl3 = [3 5 6];

jl = {jl1; jl2; jl3};

% Sum till i = 2
jli = sum(cat(1,jl{1:2}))

Oleg

Subject: add a few matrix

From: ImageAnalyst

Date: 1 Jan, 2011 23:20:49

Message: 10 of 12

Why is that not what you mean? That is what you wrote, and it sums
the matrices. Then you should give an example of what you think the
output *should* look like.

Subject: add a few matrix

From: Walter Roberson

Date: 2 Jan, 2011 00:20:51

Message: 11 of 12

On 01/01/11 4:32 PM, wahyoe Unggul wrote:
> ImageAnalyst <imageanalyst@mailinator.com> wrote in message
> <38323b18-1aef-46ed-adfa-2aefebaf4cd5@o4g2000yqd.googlegroups.com>...
>> I don't understand why you aren't even trying it in MATLAB. Just copy
>> your code from the newsreader into MATLAB and hit the run button.
>> It's as simple as that. Why are you not trying it??? Here's *your*
>> example:

>> jl1 = [2 4 5]
>> jl2 = [3 4 6]
>> jl3 = [3 5 6]
>> jli = [a b c]
>
> sorry, that's not what I mean
> a, b and c is the number
> JL = jl1 + jl2 + jl3 +.......+ jli

Are you trying to sum jl1, jl2, and so on up to jli where i here
indicates a number (e.g., if i were 17 you would want to sum to go to
jl17 ) ?

If so, then that kind of code is not advised. Please review

http://matlab.wikia.com/wiki/FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F

Subject: add a few matrix

From: wahyoe Unggul

Date: 2 Jan, 2011 05:22:04

Message: 12 of 12

thanks Walter Roberson,Oleg and ImageAnalyst

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