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:
problem with matrix

Subject: problem with matrix

From: zack00

Date: 8 Apr, 2013 13:22:36

Message: 1 of 15

I have a zero matrix with 100 collumns. I save there ploting signal from infinity loop.
i want save only last 100 collumns..Start running plot and save data to matrix.
When the matrix is filled delete first collumn and shift to left and save the last value.

Can you help me with this problem?

Subject: problem with matrix

From: dpb

Date: 8 Apr, 2013 13:24:48

Message: 2 of 15

On 4/8/2013 8:22 AM, zack00 wrote:
> I have a zero matrix with 100 collumns. I save there ploting signal from infinity loop.
> i want save only last 100 collumns..Start running plot and save data to matrix.
> When the matrix is filled delete first collumn and shift to left and save the last value.
>
> Can you help me with this problem?

Probably better than actually shifting columns would be to simply keep a
pointer to the next column to use modulo Ncols instead.

--

Subject: problem with matrix

From: Bruno Luong

Date: 8 Apr, 2013 13:44:12

Message: 3 of 15

dpb <none@non.net> wrote in message <kjuger$mqe$1@speranza.aioe.org>...
> On 4/8/2013 8:22 AM, zack00 wrote:

>
> Probably better than actually shifting columns would be to simply keep a
> pointer to the next column to use modulo Ncols instead.
>

Why it is better?

Whereas the array is rearranged later with modulo, or at the begining without, the data in the memory get moved by the same amount.

Bruno

Subject: problem with matrix

From: zack00

Date: 8 Apr, 2013 14:21:09

Message: 4 of 15

Dňa pondelok, 8. apríla 2013 15:44:12 UTC+2 Bruno Luong napísal(-a):
> dpb <none@non.net> wrote in message <kjuger$mqe$1@speranza.aioe.org>...
>
> > On 4/8/2013 8:22 AM, zack00 wrote:
>
>
>
> >
>
> > Probably better than actually shifting columns would be to simply keep a
>
> > pointer to the next column to use modulo Ncols instead.
>
> >
>
>
>
> Why it is better?
>
>
>
> Whereas the array is rearranged later with modulo, or at the begining without, the data in the memory get moved by the same amount.
>
>
>
> Bruno

but How can I do it Bruno?

Subject: problem with matrix

From: dpb

Date: 8 Apr, 2013 14:56:28

Message: 5 of 15

On 4/8/2013 8:44 AM, Bruno Luong wrote:
> dpb <none@non.net> wrote in message <kjuger$mqe$1@speranza.aioe.org>...
>> On 4/8/2013 8:22 AM, zack00 wrote:
>
>>
>> Probably better than actually shifting columns would be to simply keep
>> a pointer to the next column to use modulo Ncols instead.
>>
>
> Why it is better?
> Whereas the array is rearranged later with modulo, or at the begining
> without, the data in the memory get moved by the same amount.

Why does he ever _need_ to rearrange memory at all? All he needs is the
pointer to and use the desired column(s).

--

Subject: problem with matrix

From: dpb

Date: 8 Apr, 2013 14:58:06

Message: 6 of 15

On 4/8/2013 9:21 AM, zack00 wrote:
> Dňa pondelok, 8. apríla 2013 15:44:12 UTC+2 Bruno Luong napísal(-a):
>> dpb<none@non.net> wrote in message<kjuger$mqe$1@speranza.aioe.org>...
>>> On 4/8/2013 8:22 AM, zack00 wrote:
>>> Probably better than actually shifting columns would be to simply keep a
>>> pointer to the next column to use modulo Ncols instead.
>>
>> Why it is better?
...

> but How can I do it Bruno?

I don't see why you would ever have to actually shift the full memory
but if you're insistent

doc circshift

--

Subject: problem with matrix

From: dpb

Date: 8 Apr, 2013 16:25:26

Message: 7 of 15

On 4/8/2013 8:22 AM, zack00 wrote:
> I have a zero matrix with 100 collumns. I save there ploting signal from infinity loop.
> i want save only last 100 collumns..Start running plot and save data to matrix.
> When the matrix is filled delete first collumn and shift to left and save the last value.
>
> Can you help me with this problem?

i=0:11,mod(i,10)+1
idx=0; % index initialization
nCol=size(y,2); % array column size
while
   % Next index on range [1,nCol], increment loop counter
   idx=mod(idx,nCol)+1; idx=idx+1;

   y(:,idx)=yourfunc(whatever);
   plot(y(:,idx))
   % do whatever else...
end

One can also write the pointer logic as

idx=idx+1; if idx>nCol, idx=1; end

if like.

Whichever way you do it, for maintaining the last N computations in a
circular buffer there's no reason at all to actually shift data every
iteration.

If you want the final results saved in chronological order on
termination of the simulation, say, _THEN_ you can do the reordering once.

--

Subject: problem with matrix

From: dpb

Date: 8 Apr, 2013 16:38:19

Message: 8 of 15

On 4/8/2013 11:25 AM, dpb wrote:
...

> i=0:11,mod(i,10)+1

BTW, the above line isn't needed; I wrote it intending a tutorial then
neglected to come back and fill it in...

To see how suggested logic works, cut/paste the above at the command line.

The rest is a sample implementation w/ an infinite while...end loop
instead of for...end w/ counted index.

--

Subject: problem with matrix

From: Bruno Luong

Date: 8 Apr, 2013 17:24:10

Message: 9 of 15

OP needs to *plot* the 100 last data, in the order. So the data need to be rearranged.

Whereas he uses circlshift or indexing modulo, then the data that get moved in the memory.

At the end there is no saving compared to the strategy where the data get shifted by user:

Data = [Data(:,2:end) NewData].

Bruno

Subject: problem with matrix

From: dpb

Date: 8 Apr, 2013 17:37:03

Message: 10 of 15

On 4/8/2013 12:24 PM, Bruno Luong wrote:
> OP needs to *plot* the 100 last data, in the order. So the data need to
> be rearranged.
>

Not how I understood his simulation--looks to me like he's using each
column as the results of an iteration and just wants to save the last
100 iterations as the columns of an array.

If so, there's no reason not to do as the sample I posted earlier that
doesn't move any data unless wants to save those results in
chronological order--then he can reorder at the end before the saving
operation once instead of every iteration.

> Whereas he uses circlshift or indexing modulo, then the data that get
> moved in the memory.
>
> At the end there is no saving compared to the strategy where the data
> get shifted by user:
>
> Data = [Data(:,2:end) NewData].

If that's what he's got to do but not how I read his posting.

--

Subject: problem with matrix

From: Bruno Luong

Date: 8 Apr, 2013 17:56:09

Message: 11 of 15

dpb <none@non.net> wrote in message <kjuv7r$9cr$1@speranza.aioe.org>...
> On 4/8/2013 12:24 PM, Bruno Luong wrote:
> > OP needs to *plot* the 100 last data, in the order. So the data need to
> > be rearranged.
> >
>
> Not how I understood his simulation

OP used the words "plot" and "ploting".

Bruno

Subject: problem with matrix

From: dpb

Date: 8 Apr, 2013 19:20:49

Message: 12 of 15

On 4/8/2013 12:56 PM, Bruno Luong wrote:
> dpb <none@non.net> wrote in message <kjuv7r$9cr$1@speranza.aioe.org>...
>> On 4/8/2013 12:24 PM, Bruno Luong wrote:
>> > OP needs to *plot* the 100 last data, in the order. So the data need to
>> > be rearranged.
>> >
>>
>> Not how I understood his simulation
>
> OP used the words "plot" and "ploting".

Well, yes, my example shows plot. But, if his "array" of 100 columns is
instead a row vector then I've already allowed as how he'll have to reorder.

If this is what OP's doing, he'll want to read the section on Animation
in the documentation on 2D graphs...

--

Subject: problem with matrix

From: zack00

Date: 8 Apr, 2013 20:13:11

Message: 13 of 15

Dňa pondelok, 8. apríla 2013 21:20:49 UTC+2 dpb napísal(-a):
> On 4/8/2013 12:56 PM, Bruno Luong wrote:
>
> > dpb <none@non.net> wrote in message <kjuv7r$9cr$1@speranza.aioe.org>...
>
> >> On 4/8/2013 12:24 PM, Bruno Luong wrote:
>
> >> > OP needs to *plot* the 100 last data, in the order. So the data need to
>
> >> > be rearranged.
>
> >> >
>
> >>
>
> >> Not how I understood his simulation
>
> >
>
> > OP used the words "plot" and "ploting".
>
>
>
> Well, yes, my example shows plot. But, if his "array" of 100 columns is
>
> instead a row vector then I've already allowed as how he'll have to reorder.
>
>
>
> If this is what OP's doing, he'll want to read the section on Animation
>
> in the documentation on 2D graphs...
>
>
>
> --

I have running signal and I need only 100 last data from this signal and plot it in time..therefore need a matrix with 100 columns to save this data

Subject: problem with matrix

From: Bruno Luong

Date: 8 Apr, 2013 20:44:07

Message: 14 of 15

zack00 <p.hilovsky@gmail.com> wrote in message <f42ca2ca-d79b-477e-a335-b3a5d6ccf5e6@googlegroups.com>...
> I have a zero matrix with 100 collumns. I save there ploting signal from infinity loop.
> i want save only last 100 collumns..Start running plot and save data to matrix.
> When the matrix is filled delete first collumn and shift to left and save the last value.
>
> Can you help me with this problem?

Try this:

f = (1+rand(1,3));
a = 1+rand(1,3);
tfun = @(t) a.*sin(t.*f);

fig = figure(1);
clf(fig);
ax = axes('Parent', fig);

t = 0;
dt = 0.01;

i = 1;
filled = false;
m = length(f);
n = 100;
ft = nan(n,m);

h = plot(ax, ft);
axis(ax, [0 n -2.5 2.5]);
set(ax, 'Xtick', []);
while true
    t = t+dt;
    i = mod(i,n) + 1;
    ft(i,:) = tfun(t); % Put your function here
    filled = filled || i == 1;
    if filled
        idx = [i+1:n 1:i];
    else
        idx = 1:i;
    end
    for k=1:m
        set(h(k), 'YData', ft(idx,k));
    end
    pause(0.01);
end

% Bruno

Subject: problem with matrix

From: dpb

Date: 8 Apr, 2013 20:47:03

Message: 15 of 15

On 4/8/2013 3:13 PM, zack00 wrote:
...

> I have running signal and I need only 100 last data from this signal
> and plot it in time..therefore need a matrix with 100 columns to save
> this data

OK, my interpretation was wrong...I would call that matrix (while it is
one, yes) a [row] vector. So, while circular buffers are wondrous
things in some cases, this isn't one in the end as Bruno says.

You will have to do something like

while 1
   y(1:end-1)=y(2:end);
   y(100)=simulationresult(whatever);
   plot(x,y)
   ...
end

There are many ways to get the same result and as noted above I suggest
reading the section under the 2D graphics on 'Animation' to see details
of ways to do this that aren't quite as compute-intensive as doing the
full plot() all over again.

You can update the [X|YData] vectors instead and then REFRESHNOW for one...

--

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