From: Alex on
Hi, few beginner questions.

in matlab are arrays and matrices ultimately the same thing?

I want an array of three columns. The first column holds the time, 2nd holds a pressure, and the third a volume.
I created an array pv = ones(1000, 3);

i believe this will be an array of 1000 rows, and 3 columns.

then in a for loop

for t = 0:0.0001:10;
i = i+1;
pressure = [an equation];
volume = [an equation];

now i want to put those values for t, pressure and volume into the array pv.

pv(i,i,i) = (t,pressure, volume);

this doesnt seem to work, but i cant find a tutorial explaining something like this. Am i close?

if i then wanted to plot the pressures against volumes, could i do

plot (pressure,volume)?

do i need to use a for loop at all?

thanks

alex
From: Luca Zanotti Fragonara on


"Alex " <a.mlw.walker(a)googlemail.com> wrote in message <hkph9o$lg6$1(a)fred.mathworks.com>...
> Hi, few beginner questions.
>
> in matlab are arrays and matrices ultimately the same thing?
>
> I want an array of three columns. The first column holds the time, 2nd holds a pressure, and the third a volume.
> I created an array pv = ones(1000, 3);
>
> i believe this will be an array of 1000 rows, and 3 columns.
>
> then in a for loop
>
> for t = 0:0.0001:10;
> i = i+1;
> pressure = [an equation];
> volume = [an equation];
>
> now i want to put those values for t, pressure and volume into the array pv.
>
> pv(i,i,i) = (t,pressure, volume);
>
> this doesnt seem to work, but i cant find a tutorial explaining something like this. Am i close?
>
> if i then wanted to plot the pressures against volumes, could i do
>
> plot (pressure,volume)?
>
> do i need to use a for loop at all?
>
> thanks
>
> alex



t=0:0.0001:10;

for ii=1:length(t)
pv(:,1)=t;
pv(ii,2)=an equation f(t) that defines pressure;
pv(ii,3)=an equation f(t) that defines volume;
end

plot(pv(:,2),pv(:,3))


You can also avoid the for loop, if you can vectorize your equation:
Imagine that a and b are constant, who defines the pressure and the volume:

pv(:,2)=a*b*t;
pv(:,3)=a/b*t;

a and b can be either scalar or vectors.

Good luck,

Luca
From: Oleg Komarov on
"Alex "
> Hi, few beginner questions.
>
> in matlab are arrays and matrices ultimately the same thing?
>
> I want an array of three columns. The first column holds the time, 2nd holds a pressure, and the third a volume.
> I created an array pv = ones(1000, 3);
>
> i believe this will be an array of 1000 rows, and 3 columns.
>
> then in a for loop
>
> for t = 0:0.0001:10;
> i = i+1;
> pressure = [an equation];
> volume = [an equation];
>
> now i want to put those values for t, pressure and volume into the array pv.
>
> pv(i,i,i) = (t,pressure, volume);
>
> this doesnt seem to work, but i cant find a tutorial explaining something like this. Am i close?
>
> if i then wanted to plot the pressures against volumes, could i do
>
> plot (pressure,volume)?
>
> do i need to use a for loop at all?
>
> thanks
>
> alex

A useful tutorial:
http://www.mathworks.com/access/helpdesk/help/techdoc/math/f1-85462.html

Oleg
From: Steven Lord on

"Alex " <a.mlw.walker(a)googlemail.com> wrote in message
news:hkph9o$lg6$1(a)fred.mathworks.com...
> Hi, few beginner questions.

Welcome.

> in matlab are arrays and matrices ultimately the same thing?

Usually the term 'matrix' refers to a 2-dimensional array of numeric data,
while "blocks" of data with more than 2 dimensions or non-numeric data (like
a cell array, struct array, char array, etc) are referred to as 'arrays'.

> I want an array of three columns. The first column holds the time, 2nd
> holds a pressure, and the third a volume.
> I created an array pv = ones(1000, 3);
>
> i believe this will be an array of 1000 rows, and 3 columns.

Yes, or you could also call it a 1000-by-3 matrix.

> then in a for loop
> for t = 0:0.0001:10;
> i = i+1;
> pressure = [an equation];
> volume = [an equation];
>
> now i want to put those values for t, pressure and volume into the array
> pv.
>
> pv(i,i,i) = (t,pressure, volume);
>
> this doesnt seem to work, but i cant find a tutorial explaining something
> like this. Am i close?

Close; you just need to make the right-hand side into a vector and tell
MATLAB to assign into a rectangular piece of pv, rather than an individual
element.

pv(i, :) = [t, pressure, volume];

This assigns the 1-by-3 row vector created by [t, pressure, volume] into all
the columns of the ith row of pv.

For a tutorial that describes the basics of working with matrices and arrays
in MATLAB, look in the Getting Started guide:

http://www.mathworks.com/access/helpdesk/help/techdoc/learn_matlab/bqr_2pl.html

Once you've read through that, if you are interested in learning more, check
out this section from the User's Guide:

http://www.mathworks.com/access/helpdesk/help/techdoc/math/f1-84787.html

> if i then wanted to plot the pressures against volumes, could i do
>
> plot (pressure,volume)?

Not the way you've written the example above; the variables pressure and
volume will only contain the last entry calculated. Since you stored the
data in pv, though, you could use:

plot(pv(:, 2), pv(:, 3))

to plot using the 2nd and 3rd columns for all the rows of pv.

> do i need to use a for loop at all?

That depends. Is your expression vectorized or can you make it vectorized?

http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_prog/f8-784135.html#br8fs0d-1

--
Steve Lord
slord(a)mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ


From: Alex on
Thank you very much that helped alot, I am just looking at getting rid of my for loops because matlab runs so much faster without them if everything is vectorized.

i had a for loop like so

for t = 0:0.01:10;

equation1 = ...
equation2 = ...[something including equation1]
eqn3 = ...[something including equation2]
eqn4=...[something inlcuding eqn3]
etc

end

plot (pv(eqn(n)), pv(eqn(j))

if i want to vectorize this so to get rid of the for loop can i write

t = 0:0.01:10;
equation1 = ...
equation2 = ...[something including equation1]
eqn3 = ...[something including equation2]
eqn4=...[something inlcuding eqn3]
etc
plot (pv(eqn(n)), pv(eqn(j))

because if i can it doenst seem to work, what if those equations are time dependant but only by the step size and not the actual time now, although i do want it to be simulated over time?


"Steven Lord" <slord(a)mathworks.com> wrote in message <hkpk58$h3f$1(a)fred.mathworks.com>...
>
> "Alex " <a.mlw.walker(a)googlemail.com> wrote in message
> news:hkph9o$lg6$1(a)fred.mathworks.com...
> > Hi, few beginner questions.
>
> Welcome.
>
> > in matlab are arrays and matrices ultimately the same thing?
>
> Usually the term 'matrix' refers to a 2-dimensional array of numeric data,
> while "blocks" of data with more than 2 dimensions or non-numeric data (like
> a cell array, struct array, char array, etc) are referred to as 'arrays'.
>
> > I want an array of three columns. The first column holds the time, 2nd
> > holds a pressure, and the third a volume.
> > I created an array pv = ones(1000, 3);
> >
> > i believe this will be an array of 1000 rows, and 3 columns.
>
> Yes, or you could also call it a 1000-by-3 matrix.
>
> > then in a for loop
> > for t = 0:0.0001:10;
> > i = i+1;
> > pressure = [an equation];
> > volume = [an equation];
> >
> > now i want to put those values for t, pressure and volume into the array
> > pv.
> >
> > pv(i,i,i) = (t,pressure, volume);
> >
> > this doesnt seem to work, but i cant find a tutorial explaining something
> > like this. Am i close?
>
> Close; you just need to make the right-hand side into a vector and tell
> MATLAB to assign into a rectangular piece of pv, rather than an individual
> element.
>
> pv(i, :) = [t, pressure, volume];
>
> This assigns the 1-by-3 row vector created by [t, pressure, volume] into all
> the columns of the ith row of pv.
>
> For a tutorial that describes the basics of working with matrices and arrays
> in MATLAB, look in the Getting Started guide:
>
> http://www.mathworks.com/access/helpdesk/help/techdoc/learn_matlab/bqr_2pl.html
>
> Once you've read through that, if you are interested in learning more, check
> out this section from the User's Guide:
>
> http://www.mathworks.com/access/helpdesk/help/techdoc/math/f1-84787.html
>
> > if i then wanted to plot the pressures against volumes, could i do
> >
> > plot (pressure,volume)?
>
> Not the way you've written the example above; the variables pressure and
> volume will only contain the last entry calculated. Since you stored the
> data in pv, though, you could use:
>
> plot(pv(:, 2), pv(:, 3))
>
> to plot using the 2nd and 3rd columns for all the rows of pv.
>
> > do i need to use a for loop at all?
>
> That depends. Is your expression vectorized or can you make it vectorized?
>
> http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_prog/f8-784135.html#br8fs0d-1
>
> --
> Steve Lord
> slord(a)mathworks.com
> comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
>