i am trying to add individual integers to a certain array
my array is in this form
array= number +1
how can i make lets say number be a array = 2 +3 +1
where 2 and 3 are two different distinct obhects that should be commutatively added.
i hope i am clear. I dont want the numbers to be added so that the aboce equations would equal 0. i want it to be equal to 2 then 3 then 1....
NSMutableArray????
Do something like this:
int d = 0;
for (d = 0; i <100; i++)
{
[array addObject:[[NSString alloc] initWithFormat:#"%d", d]];
}
or whatever you want it to be
edit:
To add the numbers, just use intvalue
Related
I have a structure myS with several fields, including myField, which in turns includes several other fields such as BB. I need to count how many time *'R_value' appears in BB.
I have tried:
sum(myS.myField.BB = 'R_value')
and this:
count = 0;
for i = 1:numel(myS.myField)
number_of_element = numel(myS.myField(i).BB)=='R_value'
count = count+number_of_element;
end
but it doesn't work. Any suggestion?
If you are just checking if BB is that literal string, then your loop is just:
count = 0;
for i = 1:numel(myS.myField)
count = count+strcmp(myS.myField(i).BB,'R_value')
end
numel counts how many elements are. Zero is an element. so is False. Just sum the array.
count = 0;
for i = 1:numel(myS.myField)
number_of_element = sum(myS.myField(i).BB==R_value)
count = count+number_of_element;
end
Also note you had the parenthesis wrong, so you where counting how many BB where in total, then comparing that number to R_value. I am assuming R_value is a number.
e.g.:
myS.myField(1).BB=[1 2 3 4 1 1 1]
myS.myField(2).BB=[4 5 65 1]
R_value=1
In MatLab, I have several data vectors that are in text. For example:
speciesname = [species1 species2 species3];
genomelength = [8 10 5];
gonometype = [RNA DNA RNA];
I realise that to make a plot, arrays must be numerical. Is there a quick and easy way to assign unique entries in an array a number, for example so that RNA = 1 and DNA = 2? Note that some arrays might not be binary (i.e. have more than two options).
Thanks!
So there is a quick way to do it, but im not sure that your plots will be very intelligible if you use numbers instead of words.
You can make a unique array like this:
u = unique(gonometype);
and make a corresponding number array is just 1:length(u)
then when you go through your data the number of the current word will be:
find(u == current_name);
For your particular case you will need to utilize cells:
gonometype = {'RNA', 'DNA', 'RNA'};
u = unique(gonometype)
u =
'DNA' 'RNA'
current = 'RNA';
find(strcmp(u, current))
ans =
2
This question already has answers here:
Getting Top 10 Highest Numbers From Array?
(5 answers)
Closed 8 years ago.
I have a bunch of NSArrays filled with numbers. Is there anyway that I can somehow grab a specific number, specifically like:
Third highest number in array, or 24th highest number in array?
But I don't want to just grab the number, I also need a reference to the index it was in the array as well, if that can be retained in the process.
Thanks.
Third highest number in array, or 24th highest number in array?
Create a temporary copy of the array and sort it ascending.
Get the number at index 3-1 or 24-1.
I also need a reference to the index it was in the array as well
Now use indexOfObject: or indexOfObjectIdenticalTo: to get the actual index.
Starting with the sorted numbers array from my answer here Getting Top 10 Highest Numbers From Array? :
NSUInteger wanted = 24 or anything;
NSUInteger count = 0;
NSNumber* previous = nil;
for (NSDictionary* entry in numbers)
{
if ( wanted == 1 ) return entry;
if (previous == nil)
{
// first entry
previous = entry[#"number"];
++ count;
continue;
}
// same number? skip it.
if ( entry[#"number"] isEqualTo: previous ) continue;
// if we get here, we found a different number we may be interested in
++ count;
if ( count == wanted ) return entry;
}
// nothing found
return nil;
The result is a line in the numbers array from the previous question. An array of the form #[ #"number" : #<number>, #"parent" : <source array> ].
I am trying to comprehend how I can create multi-dimensional NSMutable arrays in general. I have come across a few solutions but haven't been able to make them work for me, so I am not sure of their validity. Now if only someone here can help me understand how to create 2-D NSMutable arrays better that would be great!
Moving to the next question, I am not sure when I should summon NSArray/NSMutableArray vs simply using a C array. In the particular case I am dealing with, I have a fixed data type
that I want to use (boolean values) and these are clearly not objects. NS and NSMutableArray are meant to hold objects, if I am not mistaken. So is this a good idea to use a regular C array vs NSMutable array?
Adding a final twist to the question on creating 2D arrays, is using NSMatrices a better alternative or even an option than creating 2D NSMutable arrays?
Thanks and major high fives to all those who read and answer this!
To create 2D array using NSMutableArrays you would need to do the following:
// Create the 2D array
// array2D is created autoreleased. It should be retained somewhere
// to keep it around.
NSMutableArray* array2D = [NSMutableArray array];
// Add a NSMutableArray to array2D for each row
NSUInteger countRows = 8; // Or whatever value you need
for (NSUInteger i = 0; i < countRows; i++) {
NSMutableArray* row = [NSMutableArray array];
[array2D addObject:row];
}
Note that you can add additional rows to array2D at any time. Also each row starts out
with size 0 and is empty. You can add different number of elements to each row so it
is a jagged 2D array rather than something more like a matrix which would be fixed size
(i.e. M rows x N columns.)
To set a value at a specific row and column you would do the following:
NSUInteger rowIndex = 5;
NSUInteger columnIndex = 7;
NSNumber* value = [NSNumber numberWithInt:11];
// Get the 6th row
// Make sure there are 6 rows
NSUInteger countRows = array2d.count;
if (countRows >= (rowIndex + 1)) {
NSMutableArray* row = (NSMutableArray*)[array2d objectAtIndex:rowIndex];
// Get the 8th column
// Make sure there are 8 columns
NSUInteger countColumns = row.count;
if (countColumns >= (columnIndex + 1)) {
// Set the value
[row setObject:value atIndex:columnIndex];
}
}
You can store C types in NSMutableArrays by wrapping them in ObjectiveC objects.
A number can be translated into an object using NSNumber. NSNumber can also wrap boolean
values. Pointers to C structs can be wrapped using NSValue objects. You can also create
NSValue objects that wrap specific Cocoa types, e.g. CGRect.
int intValue = 1;
NSNumber* number = [NSNumber numberWithInt:intValue];
BOOL boolValue = NO;
NSNumber* number = [NSNumber numberWithBool:boolValue];
NSArrays are not modifiable. If you need to add or remove objects to an array, you should you an NSMutableArray. Otherwise use a NSArray.
NSMutableArrays and NSArrays retain the objects that are added to them, taking ownership of them. When an object is removed from an NSMutableArray it is released so that it is cleaned up. When you release an NSArray or an NSMutableArray that you no longer require, they will clean up any objects that you have added to the array. This makes memory management of
objects within arrays much easier.
You can't add nil objects to NSArrays and NSMutableArrays.
NSMutableArray is dynamically resizing whilst C arrays are not. This makes it much easier
to deal with adding and removing objects to the array.
I would use a C array for a group of C types, e.g. ints that is fixed size and
whose values are known at compile time:
int knownConstantValues[] = { 1, 2, 3 };
You might need to use a C array to pass data to a library with a C API, e.g. OpenGL.
Hope this helps.
I am working on a small isometric engine for my next iPhone game. To store the map cells or tiles I need a 2 dimensionel array. Right now I am faking it with a 1-dimensionel, and it is not good enough anymore.
So from what I have found out looking around the net is that in objective-c I need to make an array of arrays.
So here is my question: How do I dynamicly create arrays at runtime based on how many map-rows I need?
The first array is easy enough:
NSMutableArray *OuterArray = [NSMutableArray arrayWithCapacity:mapSize];
now I have the first array that should contain an array for each row needed.
Problem is, it can be 10 but it can also be 200 or even more. So I dont want to manually create each array and then add it. I am thinking there must be a way to create all these arrays at runtime based on input, such as the chosen mapsize.
Hope you can help me
Thanks in advance
Peter
I think this previous question should help.
2d arrays in objective c
Nothing to do with me. I have never owned an iphone or tried to write code for one.
The whole point of NSMutableArray is that you don't care. Initialize both dimensions with an approximate size and then add to them. If your array grows beyond your initial estimate the backing storage will be increased to accomodate it. This counts for both your columns (first order array), and rows (second order array).
EDIT
Not sure what you meant in your comment. But this is one way to dynamically create a 2-dimensional mutable array in Objective-C.
NSUInteger columns = 25 ; // or some random, runtime number
NSUInteger rows = 50; // once again, some random, runtime number
NSMutableArray * outer = [NSMutableArray arrayWithCapacity: rows];
for(NSUInteger i; i < rows; i++) {
NSMutableArray * inner = [NSMutableArray arrayWithCapacity: columns];
[outer addObject: inner];
}
// Do something with outer array here
NSMutableArray can hold as many elements as you want to add to it. (Based on available heap though).
All you have to do is when you want to add an element(Array) to this mutable array you can add it using the addObject method.
So you create a MutableArray as follows:
NSMutabaleArray *outerArray = [NSMutableArray array]; // initially contains 0 elements.
[outerArray addobject:<anotherArray>];
From your rejection of the other answers I think you don't know how to add them in a loop, or am I wrong?
Try:
for (i = 0; i < mapSize; i++)
[outerArray addObject: [[NSMutableArray alloc] init]];
or, if you know or can estimate the size of the second dimension:
for (i = 0; i < mapSize; i++)
[outerArray addObject: [[NSMutableArray alloc]
initWithCapacity: your_2nd_d_size]];
Now, how you fill the arrays, i.e. where you get the contents depends on you. In one or more loops, you do:
[(NSMutableArray *)[outerArray objectAtIndex: i]
addObject: your_current_object];