Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations GregLocock on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

LOOPING

Status
Not open for further replies.

StressMan2506

Structural
Dec 19, 2004
122
I need some assistance with looping. Specifically I would like to interrogate an array to list the values in it and number of occurrences of each one. Say my array was (0.980 0.982 0.971 0.976 0.977 0.976 0.976 0.975 0.969 0.968), I would like to get a list of the form:-

0.968 1
0.969 1
0.971 1
0.975 1
0.976 3
0.977 1
0.980 1
0.982 1

The problem for me is that I am having difficulty with understanding Mathcad looping; my Mathcad knowledge is entirely self-taught.

Thanks in anticipation.


 
Replies continue below

Recommended for you

That's a binning problem. use hist

Cheers

Greg Locock

SIG:please see FAQ731-376 for tips on how to make the best use of Eng-Tips.
 
Thanks, Greg. I guess hist is a Mathcad function I'm not familiar with. I'll check it out.
 
Do so.

But, creating the interv vector automatically is going to be tricky without loops. And the only way I've thought of creating it obviates the need to use hist...

here's what i've got in pseudocode as i don't have Mathcad with loops. untested. Things may need initialising.

sx:=sort(x)

binptr:=0


for xptr:0..(length(x)-1)
if sx[xptr=interv[binptr
n[binptr:=1+n[binptr
else
binptr:=binptr+1
interv[binptr:=sx[xptr
n[binptr:=1
endif
endfor

This creates an entry in interv for each unique value in x, and then counts the number of occurences of each value.

There is a bit of ugliness for the first bin. It is valid if there is a zero present in x, not otherwsie.








Cheers

Greg Locock

SIG:please see FAQ731-376 for tips on how to make the best use of Eng-Tips.
 
Thanks once again Greg. I'll try it at work tomorrow; I don't have Mathcad at home. It's a work matter anyway...
 
Incidentally this is a nice algorithm for exploring the "Birthday paradox"

Cheers

Greg Locock

SIG:please see FAQ731-376 for tips on how to make the best use of Eng-Tips.
 
Hi Greg & "Occupant":

Thanks for taking the time to help. I now have a solution.

Regards,
Louis
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Sponsor