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:
inserting zeroes into a vector by vector comparison

Subject: inserting zeroes into a vector by vector comparison

From: Patrick

Date: 10 Jul, 2012 16:49:23

Message: 1 of 9

Hello All!

I've been having some issues with inserting elements into existing vectors.

The data I am dealing with is essentially points on a line that represents a geological fault (that part isn't incredibly important). The data occurs in steps (basically recording movement), but not all are identical, as not every element of the fault moves each time. Then there is no movement, the data basically skips that element and moves on to the next element that has actually move. I need to plot these "profiles" on one graph, and it works fine, but I need to insert zeroes wherever there is a missing element, as this is physically accurate.

I've attempted to do this by comparing the x-values of the profiles (distance along a given fault) with a regularly and identically spaced vector of the same length (physically), but it doesn't seem to work as there are a different number of elements vecorally.

I may just be missing an obvious fix, but I'd appreciate anyone's help.

Thanks!

Patrick

Subject: inserting zeroes into a vector by vector comparison

From: dpb

Date: 10 Jul, 2012 17:01:01

Message: 2 of 9

On 7/10/2012 11:49 AM, Patrick wrote:
...

> I've been having some issues with inserting elements into existing vectors.

"Inserting" as in extending the length of the vector or as changing the
value of some points within the same overall length????

...
> The data occurs in steps ... but not all are
> identical ... T[W?]hen there
> is no movement, the data basically skips that element and moves on to
> the next element that has actually move.

How is this 'skip' recorded in the data -- '0', NaN, something else???

> ... I need to insert zeroes wherever
> there is a missing element, as this is physically accurate.

It's still not clear to me how you know about "missing element"

How about a _small_ dataset and the desired result therefrom? Shouldn't
take more than 10-20 points I'd think to illustrate the problem and data
encoding, etc.

--

Subject: inserting zeroes into a vector by vector comparison

From: Patrick

Date: 10 Jul, 2012 17:16:08

Message: 3 of 9

dpb <none@non.net> wrote in message <jthn4m$ema$1@speranza.aioe.org>...
> On 7/10/2012 11:49 AM, Patrick wrote:
> ...
>
> > I've been having some issues with inserting elements into existing vectors.
>
> "Inserting" as in extending the length of the vector or as changing the
> value of some points within the same overall length????
>
> ...
> > The data occurs in steps ... but not all are
> > identical ... T[W?]hen there
> > is no movement, the data basically skips that element and moves on to
> > the next element that has actually move.
>
> How is this 'skip' recorded in the data -- '0', NaN, something else???
>
> > ... I need to insert zeroes wherever
> > there is a missing element, as this is physically accurate.
>
> It's still not clear to me how you know about "missing element"
>
> How about a _small_ dataset and the desired result therefrom? Shouldn't
> take more than 10-20 points I'd think to illustrate the problem and data
> encoding, etc.
>
> --

AH, my bad, this is my first time trying the newgroup out!

If the line has, say, 8 elements, then x would look like:

x = [1 2 3 4 5 6 7 8]

and there would be a corresponding measurement like

slip = [0.1 0.5 0.4 0.3 0.6 0.7 0.4 0.3].

But for when the data has a zero measurement (i.e. nothing occurred to the element in order to output the measurement (here on elements 3 and 4), x would look like this:

x = [1 2 5 6 7 8]

and "slip" would look like:
slip = [0.1 0.5 0.6 0.7 0.4 0.3].

I just need something that would pad x with the missing intergers, and place zeroes in the corresponding location in the "slip" vector. I hope this is clearer, my apologies if I'm still a bit obtuse.

Thanks!

Subject: inserting zeroes into a vector by vector comparison

From: dpb

Date: 10 Jul, 2012 18:32:01

Message: 4 of 9

On 7/10/2012 12:16 PM, Patrick wrote:
...

> But for when the data has a zero measurement (i.e. nothing occurred to
> the element in order to output the measurement (here on elements 3 and
> 4), x would look like this:
>
> x = [1 2 5 6 7 8]
>
> and "slip" would look like:
> slip = [0.1 0.5 0.6 0.7 0.4 0.3].
>
> I just need something that would pad x with the missing intergers, and
> place zeroes in the corresponding location in the "slip" vector. I hope
> this is clearer, my apologies if I'm still a bit obtuse.
...

OK, this is indeed "growing" the two vectors. That's something that's
easiest starting from the tail end going forward because if you insert
something from the front the rest changes position whereas from the back
the earlier part doesn't...

There's probably a fancier way, but the "straightahead" would look sotoo
(air code, you'll need to check/debug)...

for i=length(x):-1,1

Oh, there _is_ a much better way...and clever, besides... :)

Given your x and slip vectors--

s=zeros(1,x(end)); % temporary slip array of correct size
s(x)=slip; slip=s; clear s % and populate slip
x=1:x(end); % and new x

--

Subject: inserting zeroes into a vector by vector comparison

From: Patrick

Date: 10 Jul, 2012 19:12:14

Message: 5 of 9

dpb <none@non.net> wrote in message <jthsf7$s7c$1@speranza.aioe.org>...
> On 7/10/2012 12:16 PM, Patrick wrote:
> ...
>
> > But for when the data has a zero measurement (i.e. nothing occurred to
> > the element in order to output the measurement (here on elements 3 and
> > 4), x would look like this:
> >
> > x = [1 2 5 6 7 8]
> >
> > and "slip" would look like:
> > slip = [0.1 0.5 0.6 0.7 0.4 0.3].
> >
> > I just need something that would pad x with the missing intergers, and
> > place zeroes in the corresponding location in the "slip" vector. I hope
> > this is clearer, my apologies if I'm still a bit obtuse.
> ...
>
> OK, this is indeed "growing" the two vectors. That's something that's
> easiest starting from the tail end going forward because if you insert
> something from the front the rest changes position whereas from the back
> the earlier part doesn't...
>
> There's probably a fancier way, but the "straightahead" would look sotoo
> (air code, you'll need to check/debug)...
>
> for i=length(x):-1,1
>
> Oh, there _is_ a much better way...and clever, besides... :)
>
> Given your x and slip vectors--
>
> s=zeros(1,x(end)); % temporary slip array of correct size
> s(x)=slip; slip=s; clear s % and populate slip
> x=1:x(end); % and new x
>
> --
That worked wonderfully! All that needed changing was the comma in the for statement.

Thank you so much!

Patrick

Subject: inserting zeroes into a vector by vector comparison

From: dpb

Date: 10 Jul, 2012 19:21:38

Message: 6 of 9

On 7/10/2012 2:12 PM, Patrick wrote:
> dpb <none@non.net> wrote in message <jthsf7$s7c$1@speranza.aioe.org>...
...

>> for i=length(x):-1,1
>>
>> Oh, there _is_ a much better way...and clever, besides... :)
>>
>> Given your x and slip vectors--
>>
>> s=zeros(1,x(end)); % temporary slip array of correct size
>> s(x)=slip; slip=s; clear s % and populate slip
>> x=1:x(end); % and new x
...

> That worked wonderfully! ...
...

Oh course it did! <VBG> :) LOL etc., etc., ...

>> ... All that needed changing was the comma in thefor ...

You don't need no steenkin' FOR statement...that was just inline
stream-of-consciousness typing before I the actual solution dawned on
me. I just didn't go back and delete.

Throw everything but the three lines at the end out and forget you ever
saw it... :)

--

Subject: inserting zeroes into a vector by vector comparison

From: Patrick

Date: 10 Jul, 2012 19:44:12

Message: 7 of 9

dpb <none@non.net> wrote in message <jthvc9$3lp$1@speranza.aioe.org>...
> On 7/10/2012 2:12 PM, Patrick wrote:
> > dpb <none@non.net> wrote in message <jthsf7$s7c$1@speranza.aioe.org>...
> ...
>
> >> for i=length(x):-1,1
> >>
> >> Oh, there _is_ a much better way...and clever, besides... :)
> >>
> >> Given your x and slip vectors--
> >>
> >> s=zeros(1,x(end)); % temporary slip array of correct size
> >> s(x)=slip; slip=s; clear s % and populate slip
> >> x=1:x(end); % and new x
> ...
>
> > That worked wonderfully! ...
> ...
>
> Oh course it did! <VBG> :) LOL etc., etc., ...
>
> >> ... All that needed changing was the comma in thefor ...
>
> You don't need no steenkin' FOR statement...that was just inline
> stream-of-consciousness typing before I the actual solution dawned on
> me. I just didn't go back and delete.
>
> Throw everything but the three lines at the end out and forget you ever
> saw it... :)
>
> --
Haha! Funny how that works, thanks again!

Subject: inserting zeroes into a vector by vector comparison

From: Bruno Luong

Date: 10 Jul, 2012 19:59:08

Message: 8 of 9

x = [1 2 5 6 7 8]
slip = [0.1 0.5 0.6 0.7 0.4 0.3]

clear slipad;
slipad(x)=slip
xpad=1:max(x)

% Bruno

Subject: inserting zeroes into a vector by vector comparison

From: dpb

Date: 10 Jul, 2012 23:38:41

Message: 9 of 9

On 7/10/2012 2:59 PM, Bruno Luong wrote:
> x = [1 2 5 6 7 8]
> slip = [0.1 0.5 0.6 0.7 0.4 0.3]
>
> clear slipad;
> slipad(x)=slip
> xpad=1:max(x)
>
> % Bruno

 >> clear s
 >> s(x)=slip;
??? In an assignment A(I) = B, the number of elements in B and
  I must be the same.

 >>

I tried it first...unless it's a change in behavior in later versions...

Wouldn't surprise me _too_ much if automagic allocation has managed to
progress to the case but not here...

--

--

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