473,378 Members | 1,421 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Iterations, matrix replacement

Hi again, with a new question (beginner have a lot to learn). I am
doing an updating algorithm where I have to do 10 iterations and each
time need to replace the values of the matrix theta by the new
estimated thetaNew matrix, but it doesn't seem to be working, there is
no interative replacement, the values should end up being
0.6971233
0.09863044
0.1357830
0.06846318

but they're stuck at:
0.611544
0.0858034
0.146646
0.0733229

Thanks for any suggestion correction. The code below:

#include <iostream>
using namespace std;

int main(){
double xa[3][3]={{392.0,55.0},{76.0,38.0}};
double xb[3][3]={{0,0},{33,9}};
double xc[3][3]={{31.0,7.0},{0,0}};
double theta[3][3]={{0.25,0.25},{0.25,0.25}};
double n=641.0;
double thetaNew[3][3]={{0.25,0.25},{0.25,0.25}};
int k=0;
int i=0,j=0;
while (k<10){
for (i=0;i<2;i++){
for (j=0;j<2;j++){
thetaNew[i][j]=((xa[i][j])+(((xb[i][1]))*theta[i][j]/(theta[i]
[1]+theta[i][2]))+
(((xc[1][j]))*theta[i][j]/(theta[1][j]+theta[2][j])))/n;
}
}
for (i=0;i<2;i++){
for (j=0;j<2;j++){
theta[i][j]=thetaNew[i][j];
}
}
k=k+1;
cout<<"iteration: "<<k<<"\n";
}
for (i=0;i<2;i++){
for (j=0;j<2;j++){
cout <<"" <<thetaNew[i][j]<<"\n";
}
}
system("PAUSE");
return 0;
}
Apr 7 '08 #1
2 1254
On Apr 8, 12:18*am, Francogrex <fra...@grex.orgwrote:
Hi again, with a new question (beginner have a lot to learn). I am
doing an updating algorithm where I have to do 10 iterations and each
time need to replace the values of the matrix theta by the new
estimated thetaNew matrix, but it doesn't seem to be working...
OK, again, after some trial and error I figured it out, it works fine
now (see code below). The problem was that I am still confused about
the 0 subscript eg xa[0]. In S+ it doesn't exist. Now I know better.

The good code:

#include <iostream>
using namespace std;

int main(){
double xa[3][3]={{392.0,55.0},{76.0,38.0}};
double xb[3][3]={{33,33},{9,9}};
double xc[3][3]={{31,7},{31,7}};
double theta[3][3]={{0.25,0.25},{0.25,0.25}};
double n=641.0;
double thetaNew[3][3]={{0.25,0.25},{0.25,0.25}};
int k=0;
int i=0,j=0;
while (k<10){
k=k+1;
for (i=0;i<2;i++){
for (j=0;j<2;j++){
thetaNew[i][j]=((xa[i][j])+(((xb[i][0]))*theta[i][j]/(theta[i]
[0]+theta[i][1]))+
+ (((xc[0][j]))*theta[i][j]/(theta[0][j]+theta[1][j])))/n;
}
}
for (i=0;i<2;i++){
for (j=0;j<2;j++){
theta[i][j]=thetaNew[i][j];
}
}
cout<<"iteration: "<<k<<"\n";
for (i=0;i<2;i++){
for (j=0;j<2;j++){
cout<<"iteration: "<<theta[i][j]<<"\n";
}
}
}
system("PAUSE");
return 0;
}
Apr 8 '08 #2
Francogrex wrote:
On Apr 8, 12:18 am, Francogrex <fra...@grex.orgwrote:
>Hi again, with a new question (beginner have a lot to learn). I am
doing an updating algorithm where I have to do 10 iterations and each
time need to replace the values of the matrix theta by the new
estimated thetaNew matrix, but it doesn't seem to be working...

OK, again, after some trial and error I figured it out, it works fine
now (see code below). The problem was that I am still confused about
the 0 subscript eg xa[0]. In S+ it doesn't exist. Now I know better.

The good code:
I haven't bothered to test this code, but some comments
#include <iostream>
using namespace std;

int main(){
double xa[3][3]={{392.0,55.0},{76.0,38.0}};
The data is 2x2, yet you are declaring an array 3x3. Why? Why aren't you
using
double xa[2][2] = {{392.0,55.0},{76.0,38.0}};
The elements are xa[0][0], xa[0][1], xa[1][0] and xa[1][1]

Arrays in C and C++ are 0 bound.
double xb[3][3]={{33,33},{9,9}};
double xc[3][3]={{31,7},{31,7}};
double theta[3][3]={{0.25,0.25},{0.25,0.25}};
double n=641.0;
double thetaNew[3][3]={{0.25,0.25},{0.25,0.25}};
int k=0;
int i=0,j=0;
while (k<10){
k=k+1;
for (i=0;i<2;i++){
for (j=0;j<2;j++){
thetaNew[i][j]=((xa[i][j])+(((xb[i][0]))*theta[i][j]/(theta[i]
[0]+theta[i][1]))+
+ (((xc[0][j]))*theta[i][j]/(theta[0][j]+theta[1][j])))/n;
}
}
Here your counters are correctly going over the array as if it was 2x2. I
still don't know why you made them 3x3
for (i=0;i<2;i++){
for (j=0;j<2;j++){
theta[i][j]=thetaNew[i][j];
}
}
cout<<"iteration: "<<k<<"\n";
for (i=0;i<2;i++){
for (j=0;j<2;j++){
cout<<"iteration: "<<theta[i][j]<<"\n";
}
}
}
system("PAUSE");
return 0;
}


--
Jim Langston
ta*******@rocketmail.com
Apr 8 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Ben Ingram | last post by:
Hi all, I am writing a template matrix class in which the template parameters are the number of rows and number of columns. There are a number of reasons why this is an appropriate tradeoff for...
5
by: Jason | last post by:
Hello. I am trying to learn how operator overloading works so I wrote a simple class to help me practice. I understand the basic opertoar overload like + - / *, but when I try to overload more...
6
by: memocan | last post by:
#include <iostream> using namespace std; int x; //global variable matrix int main() { x= new float ; //initialize the size now }
20
by: Frank-O | last post by:
Hi , Recently I have been commited to the task of "translating" some complex statistical algorithms from Matlab to C++. The goal is to be three times as fast as matlab ( the latest) . I've...
1
by: Peterwkc | last post by:
Hello all expert, i have two program which make me desperate bu after i have noticed the forum, my future is become brightness back. By the way, my problem is like this i the first program was...
2
by: DarrenWeber | last post by:
Below is a module (matrix.py) with a class to implement some basic matrix operations on a 2D list. Some things puzzle me about the best way to do this (please don't refer to scipy, numpy and...
0
by: DarrenWeber | last post by:
# Copyright (C) 2007 Darren Lee Weber # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free...
18
by: Hypnotik | last post by:
Hello everyone. I'm writing a program which uses a class called matrix. I have written all of the different functions, constructor, etc. When I run the program I receive "Constructor", which I...
2
by: rijaalu | last post by:
I am designing a matrix class that performs addition, multicpication, substraction and division. When ever i complie the code it shows an error. include <iostream> using namespace std; class...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.