• Hey, guest user. Hope you're enjoying NeoGAF! Have you considered registering for an account? Come join us and add your take to the daily discourse.

Programming |OT| C is better than C++! No, C++ is better than C

OK still working on my base conversion program and I have to figure out how to convert to and from up to base 35. I have the program running for when decimals are used and it works fine, but now I have to figure out how to both convert from and to special characters. (a through z)

For example, converting 42 base 10 to base 16 should return 2a and not 210.

Would anyone know how to do this without using of the likes of parseInt? (isn't allowed)

My best bet when pulling the numbers out of a string (such as "4deadc0d") is to use a scanner or loop to pull out the characters individually and find their given values. (haven't implemented it yet) But on the flipside, I'm not sure how to then convert back into the characters after getting the values out of them. Basically if think my idea might be able to convert from base 16 to base 10, but I'm unaware of how to convert from base 10 back into base 16. (basically get the values back into their letter form)

have an array or list with 1,2,3.....9 and then a....z
The way I would tackle it would be to do it the same way I do it in my head. 42 base 10 to base 16 -> 42\16 = 2 remainder 10. 2 < 16 so we find the 2nd element in our array which is 2. 10 < 16 so we find the 10th element in our array which is a.

Assume we have a bigger number than our base e.g 35550 base 10 to base 35. You so 3550 \ 35 = 101 remainder 15. 15th in array is F. 101 > 35 so we recursively call the function on 101 base 10 to base 35. That gives you 2 remainder 31. 31st in array is V. Add it to the F we got before and we have 2VF.
 

Gartooth

Member
have an array or list with 1,2,3.....9 and then a....z
The way I would tackle it would be to do it the same way I do it in my head. 42 base 10 to base 16 -> 42\16 = 2 remainder 10. 2 < 16 so we find the 2nd element in our array which is 2. 10 < 16 so we find the 10th element in our array which is a.

Assume we have a bigger number than our base e.g 35550 base 10 to base 35. You so 3550 \ 35 = 101 remainder 15. 15th in array is F. 101 > 35 so we recursively call the function on 101 base 10 to base 35. That gives you 2 remainder 31. 31st in array is V. Add it to the F we got before and we have 2VF.

Wow that makes a lot of sense!

My original plan was to

  1. Scan string for letters
  2. Make sure letters are within bounds. ( ex: base 16 can't have over 'f')
  3. If letter is approved then assign value
  4. Put all letters back in string
  5. Do math to convert number to base 10, then from there to the base that is desired
  6. Then check the wanted base, see what my available letters are, and see if there are any instances where I can substitue them in

Then I would return the result, so 210 base 16 would be 2b for example.

But I think your idea is very straightforward and easier to accomplish because you use the quotient and base via a formula in order to find exactly what you need instead of my method of scanning the string every time and having to append in different values.
 
So if I wanted to teach myself to code, where would I start?

You should probably decide if you wan't to program something for the web or something for the desktop. Learning while actually building something you want is great.

In any case I'd highly recommend starting with an interpreted language. Like PHP, Javascript, Python or Ruby. Please don't listen to anyone who tells you to start with C, C++, Java or C#. While good languages they also have a lot of extra luggage that add little from a pedagogical standpoint. There are some pretty good online interactive tutorials out there like:

http://tryruby.org/
http://www.learnpython.org/
http://www.codecademy.com/
 

Troll

Banned
You should probably decide if you wan't to program something for the web or something for the desktop. Learning while actually building something you want is great.

In any case I'd highly recommend starting with an interpreted language. Like PHP, Javascript, Python or Ruby. Please don't listen to anyone who tells you to start with C, C++, Java or C#. While good languages they also have a lot of extra luggage that add little from a pedagogical standpoint. There are some pretty good online interactive tutorials out there like:

http://tryruby.org/
http://www.learnpython.org/
http://www.codecademy.com/

Looks like I'll start with Python. That was very helpful, thank you.
 

Tomat

Wanna hear a good joke? Waste your time helping me! LOL!
You should probably decide if you wan't to program something for the web or something for the desktop. Learning while actually building something you want is great.

In any case I'd highly recommend starting with an interpreted language. Like PHP, Javascript, Python or Ruby. Please don't listen to anyone who tells you to start with C, C++, Java or C#. While good languages they also have a lot of extra luggage that add little from a pedagogical standpoint. There are some pretty good online interactive tutorials out there like:

http://tryruby.org/
http://www.learnpython.org/
http://www.codecademy.com/

Extra luggage?
 
Extra luggage?

To someone just starting, do you really want to teach them:

- To assign and free memory manually?
- The PITA that is to work with strings on C/C++?
- To create header files and code files?
- To compile the code? To build Makefiles? To setup the PATH so you can make javac anywhere?
- To use an IDE? Do you really need an IDE to teach an introductory course?
- To forget all that extra stuff surrounding your code when all you want to do is a simple hello world? e.g:

Code:
public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World");
    }
}

instead of

Code:
puts "Hello World"

You are telling your student to simply ignore all those questions that naturally arise. What is public? what is class? what is static? what is main? You are exposing all those terms before even teaching him how to assign a variable ffs.

So, yeah. Extra luggage from a pedagogical point of view.
 

Tomat

Wanna hear a good joke? Waste your time helping me! LOL!
You are telling your student to simply ignore all those questions that naturally arise. What is public? what is class? what is static? what is main? You are exposing all those terms before even teaching him how to assign a variable ffs.

So, yeah. Extra luggage from a pedagogical point of view.
Can't comment on at least half of that list. What's wrong with strings? Are we talking normal character arrays without using the string library? What kind of problems have you run into?
Code:
#include <iostream>
using namespace std;

int main()
{
cout << "Hello world";
return 0;
}
puts "Hello World" is still smaller than this, but you can still do the same thing is C++ very easily.

The bolded sounds like ass backwards teaching. That is not how I learned C++ in school where we actually learned about the primitive datatypes and how to assign variables first :p

Mainly asked because I only know C++ (I have no idea how much of it I actually know. I've have two classes on it, and a general programming class where we were able to use C++ or Java). I'll be learning C# this semester though.
 
Can't comment on at least half of that list. What's wrong with strings? Are we talking normal character arrays without using the string library? What kind of problems have you run into?
Code:
#include <iostream>
using namespace std;

int main()
{
cout << "Hello world";
return 0;
}
puts "Hello World" is still smaller than this, but you can still do the same thing is C++ very easily.

The bolded sounds like ass backwards teaching. That is not how I learned C++ in school where we actually learned about the primitive datatypes and how to assign variables first :p

Mainly asked because I only know C++ (I have no idea how much of it I actually know. I've have two classes on it, and a general programming class where we were able to use C++ or Java). I'll be learning C# this semester though.

Regarding strings, when you try to do anything a little more complex with strings things usually get messier with C++. Turning a string into a date? String interpolation? Easy casting from unicode to ascii and viceversa? I'm not saying it can't be done it's just usually a lot easier to do on other languages.

Regarding the second part, even in your example you are saying "write your code inside this magical box. Ignore for now what is include, iostream, a namespace, int, etc. ". That's not even taking into account the need to have strongly typed parameters. Once again, this is only from the perspective of teaching someone new an intro course to programming.
 

missile

Member
So if I wanted to teach myself to code, where would I start?
I think the language isn't as important, since the concepts of programming are
independent of any given language. So you can likewise do a random pick of any,
buy a good book and study it till the end until going any further. Once you
got the concepts and know your direction, you may choose a language that suits
your needs, since most languages are adapted to a certain domain.
 

injurai

Banned
Python is also great for calling code of other languages if you need to break into it to have a more powerful and robust tool set.
 
What's the best resource for an artist with little to no programming experience who wants to make simple test platformer? I really want to be able to have slopes, 2 or 3 different floors at any given time, and parallax scrolling.
 

Anony

Member
I have a project that uses the kinect.
I dont need to know the kinect part, i know that what i'm getting as input is a rotation degree

what i need to do is rotate static images based on this degree (ie: a wheel that rotates)
i want it to indicate where i'm rotating, like a needle overlayed on the top of the wheel, sort of like wheel of fortune

what's the best way to approach this?

(i'm using C# and VS2010 (+ winforms i guess)
 

r1chard

Member
Yay! I got Kivy to produce something that'll run on my phone! Had a bunch of trouble with OS X compatibility in their Android build tools last time I tried.

Screw you, Lua/Moai and your shit documentation (seriously, people using Lua, documentation works)

I'm back in the land of the sane now :)
 
Yay! I got Kivy to produce something that'll run on my phone! Had a bunch of trouble with OS X compatibility in their Android build tools last time I tried.

Screw you, Lua/Moai and your shit documentation (seriously, people using Lua, documentation works)

I'm back in the land of the sane now :)

Let me know how that goes, are you developing a game or an app?
 

r1chard

Member
Let me know how that goes, are you developing a game or an app?
I've already mostly developed one game and am looking to develop two more. One of those will be the first thing I'll publish to the Store since it's a re-work of the game I already have there (but was written using the pygame subset for Android as a quick, several-hour exercise.)

The other one that I've already mostly developed will be going out to some friends for initial user testing Real Soon :)

I've not been using the "kv" language during development, but should look into it. I've found Kivy to be quite nice to work with.

Kivy's probably not so suited to developing "apps" rather than games since it doesn't provide access to the native widget set. Having said that their built-in widget set is a fair approximation of the Android Holo style.
 

rekameohs

Banned
I'm needing to transfer raw data wirelessly from one computer to another on the same network using Python. The data is generated by a computer, which will be used to control a robot.

I have very little experience with computer programming, so I was wondering how Python can be coded to do a simple transfer of numbers from one computer to another wirelessly.

Another thing that would be useful would be if I could test this system using just my computer. The finished code would run across multiple computers as mentioned, but if I could have it so my computer could act as two clients on the same network, where I could transfer the data like that, that would be fantastic. Is there a way to do this?

I'm sorry if I'm not wording this correctly, but my programming knowledge is pretty thin! My operating system is Windows 7, by the way.
 

r1chard

Member
I'm needing to transfer raw data wirelessly from one computer to another on the same network using Python. The data is generated by a computer, which will be used to control a robot.

I have very little experience with computer programming, so I was wondering how Python can be coded to do a simple transfer of numbers from one computer to another wirelessly.

Another thing that would be useful would be if I could test this system using just my computer. The finished code would run across multiple computers as mentioned, but if I could have it so my computer could act as two clients on the same network, where I could transfer the data like that, that would be fantastic. Is there a way to do this?
Do you have WiFi with a TCP/IP network on both ends of the connection, or do you need lower-level hardware support?

If you do have a viable TCP/IP network then I would recommend setting up a simple HTTP server (using http://bottlepy.org/) on the receiver and using a HTTP POST (using http://python-requests.org) on the sender.

Then to test you can have Bottle put up a simple web form and you can use that on your test computer to submit test data.
 
I've already mostly developed one game and am looking to develop two more. One of those will be the first thing I'll publish to the Store since it's a re-work of the game I already have there (but was written using the pygame subset for Android as a quick, several-hour exercise.)

The other one that I've already mostly developed will be going out to some friends for initial user testing Real Soon :)

I've not been using the "kv" language during development, but should look into it. I've found Kivy to be quite nice to work with.

Kivy's probably not so suited to developing "apps" rather than games since it doesn't provide access to the native widget set. Having said that their built-in widget set is a fair approximation of the Android Holo style.

Not to derail this thread but you should totally post on this thread instead http://www.neogaf.com/forum/showthread.php?t=491431

Some people even offer their free services there. I've been dangling with the idea of making a game but I don't think I can go back to the more traditional lower level languages. Kivy seems interesting on that regard.
 

usea

Member
I'm needing to transfer raw data wirelessly from one computer to another on the same network using Python. The data is generated by a computer, which will be used to control a robot.

I have very little experience with computer programming, so I was wondering how Python can be coded to do a simple transfer of numbers from one computer to another wirelessly.

Another thing that would be useful would be if I could test this system using just my computer. The finished code would run across multiple computers as mentioned, but if I could have it so my computer could act as two clients on the same network, where I could transfer the data like that, that would be fantastic. Is there a way to do this?

I'm sorry if I'm not wording this correctly, but my programming knowledge is pretty thin! My operating system is Windows 7, by the way.
When you say control a robot, how immediate does this control have to be? Is the communication only one-way, or does the computer receive input or something back from the robot?
 
How is this as an XNA tutorial? I'm not even very interested in making an RPG, but is it good technique to follow?

I know it's a lot to ask for someone to go check it out. His project example at the end is probably a good representation, though it seems unfinished and I'm unimpressed with the resulting build...unless he spends most of the time setting up classes to make later things easier. He starts right off the bat making a separate library to manage input and a state machine, which does seem like a good starting point.
 

Mordeccai

Member
Hi guys, I'm taking an introductory course to Python and Unix and am having trouble with an assignment. We're currently just using basic Unix commands and I'm doing this through Cygwin seeing as I'm on windows.

My prof has given me a haiku .txt file that he wants us to run diff against our own .xt file that we're supposed to make in a text editor. Same exact text, only thing to worry about are the line endings.

Now I used notepad++ to copy the haiku exactly, clicked into preferences through notepad++, and made the format Unix/OSX. I place the file into my directory and run the diff command and get two things.

1d0 read out which I don't need to worry about because the instructor tells us this is a difference between how his mac and our windows PC's may use the file system differently

but I also get 95c94 which confuses me because when I type in wc for the files they read the exact same thing, 4 13 82 so there aren't even 94 lines in either file?

Just hoping someone can shed some light on what I'm doing wrong. I suspect its still something to do with line endings.
 

arit

Member
Hi guys, I'm taking an introductory course to Python and Unix and am having trouble with an assignment. We're currently just using basic Unix commands and I'm doing this through Cygwin seeing as I'm on windows.

My prof has given me a haiku .txt file that he wants us to run diff against our own .xt file that we're supposed to make in a text editor. Same exact text, only thing to worry about are the line endings.

Now I used notepad++ to copy the haiku exactly, clicked into preferences through notepad++, and made the format Unix/OSX. I place the file into my directory and run the diff command and get two things.

1d0 read out which I don't need to worry about because the instructor tells us this is a difference between how his mac and our windows PC's may use the file system differently

but I also get 95c94 which confuses me because when I type in wc for the files they read the exact same thing, 4 13 82 so there aren't even 94 lines in either file?

Just hoping someone can shed some light on what I'm doing wrong. I suspect its still something to do with line endings.

You might consider using Virtualbox/vmware and running a virtual linux under windows, makes OS courses much easier (shared folders or just dropbox for synchronization).

And yes, windows handles line endings different than linux. In linux LF character is sufficient while windows uses CR+LF. I think notepad++ has a setting where it displays those characters.
 

amaron11

Banned
I've been using AutoCAD since 1996, so when I started this job in 2009 I looked into the .NET side of AutoCAD programming and have been doing it ever since on VB.NET.

Next step is learning C# and migrating my code over.
 

rekameohs

Banned
Do you have WiFi with a TCP/IP network on both ends of the connection, or do you need lower-level hardware support?

If you do have a viable TCP/IP network then I would recommend setting up a simple HTTP server (using http://bottlepy.org/) on the receiver and using a HTTP POST (using http://python-requests.org) on the sender.

Then to test you can have Bottle put up a simple web form and you can use that on your test computer to submit test data.

Yeah, it's over Wifi.

So I followed the "Hello Word" in a bottle tutorial on http://bottlepy.org/, and Python reads:
Code:
Bottle v0.12-dev server starting up (using WSGIRefServer())...
Listening on http://localhost:8080/
Hit Ctrl-C to quit.

So how do I get data to transfer while it's listening? I looked at the code on http://docs.python-requests.org/en/latest/, specifically at https://gist.github.com/973705, which displays:

Code:
200
application/json; charset=utf-8

I'm just wondering how I can split my screen into two windows, type a string into one window, and have it appear in the other one.

A quick youtube search comes up with this video, but that just comes up with an error:
Code:
Traceback (most recent call last):
 File "C:\Python27\Client.py", line 36, in <module>
 data = s.recv(10000) 
error: [Errno 10053] An established connection was aborted by the software in your host machine

When you say control a robot, how immediate does this control have to be? Is the communication only one-way, or does the computer receive input or something back from the robot?

It's one-way. The computer needs to give 7 values, which are rotations of the 7 joints in the robot. What this code is doing is actually just going into Blender, which has a 3d model of the robot that will rotate using the 7 values.

EDIT: Wait a minute, I fiddled around, and I may have gotten things to work.... Let me just see if I can figure things out here
 

Chris R

Member
I've been using AutoCAD since 1996, so when I started this job in 2009 I looked into the .NET side of AutoCAD programming and have been doing it ever since on VB.NET.

Next step is learning C# and migrating my code over.

Migrating to C# from VB.Net should be really, really easy. Outside of a few VB functions that C# doesn't support I'm struggling to think of what else you might have to change other than syntax.
 

roddur

Member
need some help with crystal report.

here's what i'm trying to do:

i'm loading a report that shows from today(or any day) to next 14 days info AND previous 7 (or any number) days info. as usual the first/default page of the total report shows the oldest day first. i want to be able to show the page where it shows today (or the day i selected) as the first/default page. then i can go back/forward if i want.

is this possible?

i'm using vb.net 2003.

Edit: can it be done by searching a particular text? do i need to write a formula for that? so, when report loads i'll be looking at the page i want as default.
 

usea

Member
If I'm understanding you correctly, you have a collection of records that spans both into the past and future. And on a page, you want to automatically navigate to the first page containing today's records, but still allow them to go forward/back pages after that.

I don't know anything about crystal reports or vb.net. But break your problem down into sub-problems. Say you have a collection of records ordered by date. The first problem is to find the first record that occurred today. You can do DateTime.Now.Date which will give you midnight today (in the computer's local time; use DateTime.UtcNow.Date if your records' dates are in UTC). Then iterate over the records, comparing their time to your midnight time until you find one that is greater-than or equal to midnight today. That's your first record occurring today. Here's some sample code in basic C# (sorry I don't know VB.net)

Code:
public class Thing
{
	public int Id;
	public string Name;
	public DateTime Timestamp;
}

public int FirstIndexOfToday(IList<Thing> things)
{
	var midnightToday = DateTime.Now.Date;
	for (int i = 0; i < things.Length; i++)
	{
		if(things[i].Timestamp >= midnightToday)
		{
			return i;
		}
	}
	return things.Length;
}

Your second problem is, given a collection of records and a page size, determine which page on which record N appears. The index of your record will be 0-index, while the page number result will be 1-index.

Code:
public int GetPage(int pageSize, int recordIndex)
{
	return (recordIndex / pageSize) + 1;
}
I think vb.net uses different rules for truncating after division. You might have to use \ instead of /

The last sub-problem is figuring out how to make crystal reports page to a particular page on load. I don't know how to do this.

Maybe I'm misunderstanding the problem though. Are you displaying records on pages that the user can navigate forward/page i.e. pagination? Or are you creating static pages of records, like for printing?

If you just want to order the records so today's records are first, then the rest of the records are ordered by date, then you will have to do something different. Get midnight today and tomorrow, and sort records first whether they're between those two dates, then normally by date.
 

Cindres

Vied for a tag related to cocks, so here it is.
Can anyone recommend a really good book on Android programming? I already know some, but it's been a year since I had to do any and I'd like a really good, solid, resource for learning this stuff now as I need it for a module this semester.

Kindle preferable, or any really solid online materials would be great too.

EDIT: WOW They've hugely changed the android dev site since I was last on there.
 
Okay code GAF I need help again:

How do you add an array integer into another array integer?

Example: I have multiple array's that contain different integers. I would like to add the odd positioned integer arrays into a variable.

Code:
for (int i =0; i < mCode.length(); i++)
{
	int number = Integer.parseInt(new String(stringArray));
	System.out.println(stringArray[i]);
	int odd = stringArray[i];
	System.out.println(" ");				
}
			
System.out.println("The first odd number is " + stringArray[1]);
System.out.println("The second odd number is " + stringArray[3]);
System.out.println("The third odd number is " + stringArray [5]);

I want to add stringArray[1] into stringArray[3]. How would I do this? I tried doing System.out.println("The total of the odd numbers is " + stringArray[1 + 3]); But that just adds 3 into the number that is in the second array(1).

Also, is there an automatic way to split the odd based arrays from the even ones (not the actual integers), instead of manually doing it?

Thanks a bunch!
 

usea

Member
I am not sure what you mean. It looks like you have an array of strings, and you want to find the sum of the odd-indexed elements of the array (elements 1, 3, 5 etc). So for example, if you have an array with these numbers: {1,2,3,4,5,6,7} then the sum would be 2+4+6, or 12. This is because arrays are zero-indexed. So the first element is 0, the second element is 1, etc. If you want to sum the first, third, fifth etc elements, you'll need to change this.

Like you noticed, you can't simply add strings together as if they were numbers; you have to parse them into integers first. Here is how I would modify your code to do what I think you're asking.

Code:
int sum = 0;
for(int i = 0; i < stringArray.length(); i++)
{
	boolean isOdd = (i % 2 == 1); //this will contain a boolean telling you whether the index (i) is odd or not.
	if(isOdd == true)
	{
		System.out.println(stringArray[i] + " is at an odd index"); //print out the string just to see what all the odd ones are
		var nextOddNumber = Integer.parseInt(stringArray[i]); //transform the element from a string to an integer
		sum = sum + nextOddNumber; //add the integer to the running total
	}
}
System.out.println("The sum of the odd-index numbers is " + sum); //print out the total

There are lots of ways to do this. For example, instead of making a boolean isOdd using that formula, you could change the for-loop to increment by 2 every time so that "i" is always odd.

edit: reorganized some thoughts on odd/even and array indexing.
 

That is exactly what I was asking for and helps me understand it a bit more. Thank you.

I am a complete beginner when it comes to programming so please excuse my ignorance. Also I get certain unknown errors when I try to run the program. When I execute my program it asks for the the bar code input. I enter: 1234567. Then it just stop from there and nothing else really happens. It is as if it never hit the for loop. Here is the whole code.
Code:
	public static void main(String[] args) {
		
		System.out.println("Please enter a bar code number that is atleast 5 digits");

		
		Scanner barCode = new Scanner(System.in);
		String mCode = barCode.nextLine();	//The bar code number that the user inputs. In string value.
		
		
		if (mCode.length() >= 5 )
		{
			
String[] stringArray = new String[] {mCode};	//Using a char to take each digit from a String into a char and then turn it into a char array
System.out.println("Great. You entered: " + mCode + " which contained " + mCode.length() + " digits");	//Displays the bar code they entered and the total length of the bar code
	//Turns the bar code into a char array (stringArray).
System.out.println(mCode);	//Test to see if it prints out the bar code.
			

int sum = 0;
for(int i = 0; i < mCode.length(); i++)
{
boolean isOdd = (i % 2 == 1); //this will contain a boolean telling you whether the             index (i) is odd or not.
	if(isOdd == true)
	{
		System.out.println(stringArray[i] + " is at an odd index"); //print out the string just to see what all the odd ones are
		int nextOddNumber = Integer.parseInt(stringArray[i]); //transform the element from a string to an integer
		sum = sum + nextOddNumber; //add the integer to the running total
	}
}
System.out.println("The sum of the odd-index numbers is " + sum); //print out the total
 
Labombadog, I would do something like this:

Code:
	public static void main(String[] args) {
		System.out.println("Please enter a bar code number that is at least 5 digits");

		Scanner barCode = new Scanner(System.in);
		String mCode = barCode.next();
		
		if (mCode.length() < 5) {
			System.out.println("The bar code number needs to be at least 5 digits");
			System.exit(0);
		}
		
		System.out.println("Great. You entered: " + mCode + " which contained " + mCode.length() + " digits");
		
		int sum = 0;
		for (int i = 1; i < mCode.length(); i = i+2) {
			sum += Character.getNumericValue(mCode.charAt(i));
		}
		
		System.out.println("The sum of the odd-index numbers is " + sum);
	}

Edit: The reason why your code is not working is because String[] stringArray = new String[] {mCode} will create a String array containing only one element, which is the bar code number.
 

roddur

Member
If I'm understanding you correctly, you have a collection of records that spans both into the past and future. And on a page, you want to automatically navigate to the first page containing today's records, but still allow them to go forward/back pages after that.

........

Maybe I'm misunderstanding the problem though. Are you displaying records on pages that the user can navigate forward/page i.e. pagination? Or are you creating static pages of records, like for printing?

If you just want to order the records so today's records are first, then the rest of the records are ordered by date, then you will have to do something different. Get midnight today and tomorrow, and sort records first whether they're between those two dates, then normally by date.

thanx for ur reply.

you understood my problem right. and yes they are static pages of records.

all the records are sorted by date and not all dates have same no. of records, and not all dates have records in some cases.

i'm leaning towards searching for a particular text and jumping to the page where the text is. i don't know it's the right solution or not.
 

defel

Member
Can anyone help with matlab here?

I have an nxn square matrix. I want to create another nxn matrix where each element (i,j) is equal to "1" if that element is the maximum value in the jth row and "0" otherwise.

I tried this

Code:
%W is the nxn matrix containing the values
%P is the new matrix of ones and zeros that I want to define
for i=1:n;
    
    for j=1:n;
        

        if W(i,j)=max(W,[],2) %if the element is the maximum value in each row 
            P(i,j)=1; %then let the i,jth element equal 1
            
        else
            P(i,j)=0
            
        end;
        
    end
    
end

but clearly something is wrong here.

What the best way to do this?

Thanks
 
Can anyone help with matlab here?

I have an nxn square matrix. I want to create another nxn matrix where each element (i,j) is equal to "1" if that element is the maximum value in the jth row and "0" otherwise.

[/CODE]

but clearly something is wrong here.

What the best way to do this?

Thanks

I am far from a Matlab/Octave expert but what you want to do is vectorize that. This works in Octave and should work in Matlab as well.

Code:
P = W >= repmat(max(W')', 1, n)

You can check if it's working with this:

Code:
W = rand(5,5);
P = zeros(5,5);
n = 5;

W
P = W >= repmat(max(W')', 1, n)

max(W) will give the max of each column. So max(W')' gives us a column vector of the max of each row. Repeat that for n columns with repmat and then the vectorized conditional operator in matlab will do the rest. Hope that helps!
 
Labombadog, I would do something like this:

Code:
...

Edit: The reason why your code is not working is because String[] stringArray = new String[] {mCode} will create a String array containing only one element, which is the bar code number.

That worked perfectly. I still have ways to go to finish this project but much appreciate for those who have helped me out. Hopefully I can get the grasp of things.

How long did it take people to get adjusted to coding?
 

usea

Member
How long did it take people to get adjusted to coding?
I've been doing it a little bit since I was about 8, so I don't really remember. At some point early on I got comfortable with the basics like variables, functions, arrays and loops. It wasn't until college though (20 years later) in classes like assembly and data structures that I really started to understand everything though.
 

Haly

One day I realized that sadness is just another word for not enough coffee.
About 2-3 years. I got started in high school and it was only until my second semester that things really fell into place.
 

Slavik81

Member
How long did it take people to get adjusted to coding?
Despite writing occasionally programs in various courses while growing up, I never really understood coding until I took an introduction to programming in university.

Instead of teaching programming as 'type x so y will happen', they taught fundamental computer concepts like memory and pointers. Then, they moved to teaching the c syntax as an expression of those concepts. That was probably the turning point for me.

It still took me about 1 year of occasional programming in school and 1-2 years of full time development to really felt I knew what I was doing in moderate sized programs. In small programs, a few years of occasional programming and a handful of classes was enough to mostly get the hang of things.
 
I've been doing it a little bit since I was about 8, so I don't really remember. At some point early on I got comfortable with the basics like variables, functions, arrays and loops. It wasn't until college though (20 years later) in classes like assembly and data structures that I really started to understand everything though.

Yep. Learning Assembly really helps understand what exactly is going on under the hood.

Finally taking a class on OpenGl as an elective for my major. Looking forward to it. And the operating systems class too. The others, meh.
 
First year programming class question. Really easy, I am just not too bright it seems.

What is the Cartesian Product of AxBxC if A = {1}, B = {i, j}, C = {R, T, Q}?

would it just be AxBxC = {(1,i R), (1, j R), (1, i, T), (1, j, T), (1, i Q), (1, j, Q)}

if they were all triples I would know what to do but since its singleton, pair, triple I am confused :(

Also, I know this is "math" but its a CIS course *shrug*
 
Top Bottom