Multidimensional array to string with each sub-array on new line - arrays

I'm trying to convert a multidimensional array like this:
var array = [["ID","Cue","Target","InitialCopyLen","InitialCopyStrict","NumStudied","NumTested","NumDropped","studyCountLen","testCountLen","studyCountStrict","testCountStrict","finaltestLen","finaltestStrict"],["bl","AGLUK","JAW","0","0","0","0","0","0","0","0","0","0","0"],["bl","AKI","MONEY","0","0","0","0","0","0","0","0","0","0","0"]];
to something like this (as a string):
ID Cue Target InitialCopyLen InitialCopyStrict NumStudied NumTested NumDropped studyCountLen testCountLen studyCountStrict testCountStrict finaltestLen finaltestStrict
bl AGLUK JAW 0 0 0 0 0 0 0 0 0 0 0
bl AKI MONEY 0 0 0 0 0 0 0 0 0 0 0
I tried converting to a string first, then using a replace function:
var newString = array.toString(array);
newString = newString.replace(/.{14}/g, '$&\n');
But obviously this just does every 14th character, and I need every 14th comma.
I then thought about something like this:
var i;
var commaCount = 0;
for (i = 0; i < newString.length; i++) {
if (i == ","){
commaCount++
if (commaCount == "14"){
// INSERT NEW LINE HERE
}
}
}
x.innerHTML = newString;
But perhaps there is a smarter way to do this? I basically want to convert the entire big array into a string, with each sub-array on its own line.

Inspite of iterating through every element in the inner array ,You can use for-each loop to separately convert each part of array to String.
for(var element : array){
str = element.toString();
System.out.println(str);
}
System.out.println() will take care of "\n"

I found the answer:
array.forEach(function(element) {
x.innerHTML += element + "<br>";
});`

Related

Changing an element in a multidimensional array doesen't work in Ruby

I made a class that represents a matrix using a multidimensional array. The initialize method goes through every position and sets the value of the elements to the one specified, and the to_s method does the same but it just concatenates every element to a string. I'm writing an insert method that changes the value of the element of a given position to a given value, but it changes a whole value instead of just an element.
class Matrix
attr_accessor :rows, :cols, :arr
# Goes through the matrix setting everything with the "n" value
def initialize(r, c, n = "a")
#rows = r
#cols = c
#arr = Array.new(#rows)
tmpArray = Array.new(#cols)
i = 0
while i < #rows
j = 0
while j < #cols
tmpArray[j] = n
j += 1
end
#arr[i] = tmpArray
i += 1
end
return #arr
end
def to_s
i = 0
str = String.new
while i < #rows
j = 0
str << "("
while j < #cols
str << " "
if #arr[i][j].is_a?String
str << #arr[i][j]
else
str << #arr[i][j].to_s
end
j += 1
end
str << " )\n"
i += 1
end
return str
end
# Calls and prints to_s
def write
print self.to_s
return self.to_s
end
# Rewrites the element (r, c) as the value in "n"
def insert(r, c, n)
#arr[r][c] = n
self.write
return self
end
end
The thing is that, when I print the matrix I notice that I didn't change just an element, but a whole column of the matrix.
a = Matrix.new(2, 2, 0)
a.insert(0, 0, 1)
a.write
# Expected output: ( 1 0 )
# ( 0 0 )
# Real output: ( 1 0 )
# ( 1 0 )
The to_s method isn't failing. I already made a trace of it and tested it. I'm printing the real values that are in the positions of the matrix.
Your program calls Array.new only twice, and it doesn't create arrays using any other methods, and it doesn't use dup or clone to copy any arrays. Therefore, you only have two different array objects in your program. Each element of #arr actually points to the same row array.
One idea for a solution would be to replace this line:
#arr[i] = tmpArray
with this:
#arr[i] = tmpArray.dup
However, your code is pretty long and I have not tried running it, so I do not know if that would be a complete solution.

How can I find the nonzero values in a MATLAB cells array?

The following code generates an cell array Index [1x29], where each cell is an array [29x6]:
for i = 1 : size(P1_cell,1)
for j = 1 : size(P1_cell,2)
[Lia,Lib] = ismember(P1_cell{i,j},PATTERNS_FOR_ERANOS_cell{1},'rows');
Index1(i,j) = Lib % 29x6
end
Index{i} = Index1; % 1x29
end
How can I find the nonzero values in Index array?, i.e. generate an array with the number of non-zero values in each row of the Index1 array. I tried the following loop, but it doesn't work, it creates conflict with the previous one:
for i = 1 : length(Index)
for j = 1 : length(Index)
Non_ceros = length(find(Index{:,i}(j,:))); %% I just need the length of the find function output
end
end
I need help, Thanks in advance.
The nnz() (number of non-zeros) function can be used to evaluate the number of non-zero elements. To obtain the specific positive values you can index the array by using the indices returned by the find() function. I used some random test data but it should work for 29 by 6 sized arrays as well.
%Random test data%
Index{1} = [5 2 3 0 zeros(1,25)];
Index{2} = [9 2 3 1 zeros(1,25)];
Index{3} = [5 5 5 5 zeros(1,25)];
%Initializing and array to count the number of zeroes%
Non_Zero_Counts = zeros(length(Index),1);
for Row_Index = 1: length(Index)
%Evaluating the number of positive values%
Array = Index{Row_Index};
Non_Zero_Counts(Row_Index) = nnz(Array);
%Retrieving the positive values%
Positive_Indices = find(Array);
PositiveElements{Row_Index} = Array(Positive_Indices);
disp(Non_Zero_Counts(Row_Index) + " Non-Zero Elements ");
disp(PositiveElements{Row_Index});
end
Ran using MATLAB R2019b
for i = 1 : length(Index)
for j = 1 : length(Index)
Non_ceros(i,j) = nnz(Index{:,i}(j,:));
end
end

How do you find the length of an array in V+

If i have an array, like the following:
my.arr[0] = 0;
my.arr[5] = 5;
How do i find the length?
You can find the last index by using LAST, ie:
LAST(my.arr[])
would give you:
5
so the length could be found like this:
length = LAST(my.arr[]) + 1
but you would use it in a for loop like this:
FOR i = 0 to LAST(my.arr[])
IF DEFINED(my.arr[i]) THEN
TYPE my.arr[i]
END
END

How to populate multidimensional array in Perl?

I'm getting a Use of uninitialized value error. I don't know if I'm populating my multidimensional array correctly.
my #matrix;
for (my $i=1; $i<=3;$i++){
$matrix[$i][0] = 4;
}
for (my $j=1; $j<=3;$j++){
$matrix[0][$j] = 4;
}
print $matrix[0][0];
I don't understand why this doesn't work. The way I wrote it, the matrix is supposed to populate like so:
1 0
2 0
3 0
0 1
0 2
0 3
You're populating $matrix[1][0] and $matrix[0][1], but you don't store anything in $matrix[0][0].
Perl arrays start at 0 - try
my $i = 0

MATLAB: Finding the entry number of the first '1' in a logical array

I have created a logical array of 1's and 0's using the following code:
nWindow = 10;
LowerTotInitial = std(LowerTot(1:nWindow));
UpperTotInitial = std(UpperTot(1:nWindow));
flag = 0;
flagArray = zeros(length(LowerTot), 1);
for n = 1 : nData0 - nWindow
for k = 0 : nWindow - 1
if LowerTot(n + k) < 0.1*LowerTotInitial || UpperTot(n + k) < 0.1*UpperTotInitial
flag = 1;
flagArray(n) = 1;
else
flag = 0;
end
end
end
This returns flagArray, an array of 0's and 1's. I am trying to find the index of the first 1 in the array. ie. 1 = flagArray(index). I am confused as to what is the best way to accomplish this!
What you call an entry number is referred to as an index in MATLAB-speak. To find the index of the first matching element in an array you can use the FIND function:
>> x = [0 0 1 0 1 0];
>> find(x, 1, 'first')
ans =
3
Try this ind = find(flagArray, k, 'first')
with k =1
Read this Matlab Docs - find

Resources