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

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

Related

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

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

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>";
});`

Matlab: Numerical array index into a string array (without loops)

I'm doing a set of problems from the MATLAB's introductory course at MIT OCW. You can see it here, it's problem number 9, part g.iii.
I have one matrix with the final grades of a course, all of them range from 1 to 5. And I have another array with only letters from 'F' to 'A' (in a 'decreasing' order).
I know how to change elements in a matrix, I suppose I could do something like this for each number:
totalGrades(find(totalGrades==1)) = 'F';
totalGrades(find(totalGrades==2)) = 'E';
totalGrades(find(totalGrades==3)) = 'C';
totalGrades(find(totalGrades==4)) = 'B';
totalGrades(find(totalGrades==5)) = 'A';
But then, what's the purpose of creating the string array "letters"?
I thought about using a loop, but we're supposed to solve the problem without one at that point of the course.
Is there a way? I'll be glad to know. Here's my code for the whole problem, but I got stuck in that last question.
load('classGrades.mat');
disp(namesAndGrades(1:5,1:8));
grades = namesAndGrades(1:15,2:size(namesAndGrades,2));
mean(grades);
meanGrades = nanmean(grades);
meanMatrix = ones(15,1)*meanGrades;
curvedGrades = 3.5*(grades./meanMatrix);
% Verifying
nanmean(curvedGrades)
mean(curvedGrades)
curvedGrades(curvedGrades>=5) = 5;
totalGrades = nanmean(curvedGrades,2);
letters = 'FDCBA';
Thanks a lot!
Try:
letters=['F','D','C','B','A'];
tg = [1 2 1 3 3 1];
letters(tg)
Result:
ans = FDFCCF
This works even when tg (total grade) is a matrix:
letters=['F','D','C','B','A'];
tg = [1 2 1 ; 3 3 1];
result = letters(tg);
result
result =
FDF
CCF
Edit (brief explanation):
It is easy to understand that when you do letters(2) you get the second element of letters (D).
But you can also select several elements from letters by giving it an array: letters([1 2]) will return the first and second elements (FD).
So, letters(indexesArray) will result in a new array that has the same length of indexesArray. But, this array has to contain numbers from 1 to the length of letters (or an error will pop up).

How to find a substring in an array of strings in matlab?

I have a string 'ADSL'. I want to find this string in an array of strings char('PSTN,ADSL','ADSL,VDSL','FTTH,VDSL')
when i run this command
strmatch('ADSL',char('PSTN,ADSL','ADSL,VDSL','FTTH,VDSL'));
the output is 2
But I expect the output as [1 2]
strmatch only gives positive result if the search string appears at the begining of row.
How can I find the search string if it occurs anywhere in the row?
Given the following input:
array = {'PSTN,ADSL', 'ADSL,VDSL', 'FTTH,VDSL'};
str = 'ADSL';
We find the starting position of each string match using:
>> pos = strfind(array, str)
pos =
[6] [1] []
or
>> pos = regexp(array, str)
pos =
[6] [1] []
We can then find the indices of matching strings using:
>> matches = find(~cellfun(#isempty,pos))
matches =
1 2
For an array of strings, it's better to use a cell array. That way strings can be of differnet lengths (and regexp can be applied on all cells at once):
cellArray = {'PSTN,ADSL','ADSL,VDSL','FTTH,VDSL'};
str = 'ADSL';
Then:
result = find(~cellfun('isempty', regexp(cellArray, str)));
will give what you want.
If you really have a char array as in your example,
array = char('PSTN,ADSL','ADSL,VDSL','FTTH,VDSL');
you can convert to a cell array (with cellstr) and apply the above:
result = find(~cellfun('isempty', regexp(cellstr(array), str)));
i would use strfind
a=strfind(cellstr(char('PSTN,ADSL','ADSL,VDSL','FTTH,VDSL')),'ADSL');
in this case will be a three by one cell array containing the index where you string starts at in the corresponding string

Adding elements to an array

Say that I have an empty array as follows:
s=[];
Say that for instance we have the following loop:
for j=1:2
for i=1:10
if a(i,j)>0
...
end
end
end
Instead of ..., I want to add elements to s. How do you do that in MatLab?
I would recommend you avoid loops altogether. They are slow in MATLAB.
Let's say you want to set all values in S(i,j) to 1 that correspond to A(i,j) > 0. You could do:
S = zeros(size(A)); % always a good idea to initialize your array
S(A > 0) = 1; % and done.
More succinctly:
S = A > 0;
This specifies that you are changing the values of S to 1 corresponding to those values of A where A > 0.
If you want to set the value of S to the corresponding value of A then you would just use:
S = A(A > 0);
Keep track of another index and just add elements as you go along:
idx = 1
for j=1:2
for i=1:10
if a(i,j)>0
s(idx) = a(i,j)
idx = idx + 1
end
end
end
Though for your particular problem, you could just write
a(a>0)

Resources