I currently have a large single row array of chars... I also have two arrays, the first array has all the start indexes of data I would like to retrieve from the char array, the second array has all the end indexes for the data. How can I retrieve all these wanted values from my char array without using a loop?
So far I have tried doing
chararray(1,start(:):end(:))
but this will only retrieve the first value I would like!
Cheers!
Try this -
chararray(bsxfun(#plus,start1(:)-start1(1),start1(1):end1(1)))
This would create a 2D char array where each row be the output from each iteration of your loop code.
Also, please note that I am using start1 and end1 to represent your start and end arrays respectively, so as not to create a clash with the reserved terminate scope end used by MATLAB.
Related
I have my original array that has 85 arrays, and i can access to them like this
da1[34]
The problem is that I have a list (aa) where I have the specific index for the arrays I need. I want to make a loop to append all that arrays in a new list, so i used this code
aa=[45,76,18,34]
for i in aa:
orden=[]
orden.append(da1[i])
But I only get an array, I don't know what I'm doing wrong.
What I hope is to get the same number of arrays as the number of indexes that I have in aa
You just need to define the list outside the loop, like this:
aa=[45,76,18,34]
orden=[]
for i in aa:
orden.append(da1[i])
The reason is you are creating the list every time you iterate in the for. So at the end you end up only with the last element in the list.
I have a 60x60x35 array and would like to calculate the Wilcoxon signed rank test to calculate if the median for each element value across the third array dimension (i.e. with 35 values) is different from zero. Thus, I would like my results in two 60x60 arrays - with values of 0 and 1 depending on the test statistic, and in a separate array with corresponding p values.
The problem I am facing is specifying the command in a way that desired output would have appropriate dimensions and would be calculated across the appropriate dimension of the array.
Thanks for your help and all the best!
So one way to solve your problem is using a nested for-loop. Lets say your data is stored in data:
data=rand(60,60,35);
size_data=size(data);
p=zeros(size_data(1),size_data(2));
p(:,:)=NaN;
h=zeros(size_data(1),size_data(2));
h(:,:)=NaN;
for k=1:size_data(1)
for l=1:size_data(2)
tmp_data=data(k,l,:);
tmp_data=reshape(tmp_data,1,numel(tmp_data));
[p(k,l), h(k,l)]=signrank(tmp_data);
end
end
What I am doing is I preallocate the memory of p,h as a 60x60 matrix. Then I set them to NaN, so if you can easily see if sth went wrong (0 would be an acceptable result). Now I loop over all elements and store the actual data array in a new variable. signrank needs the data to be an array so I reshape it to two dimensions.
I guess you could skip those loops by using bsxfun
How can I access the index of one character array and store those indexes into another array?
I need this array of indices so as to perform an addition with another array.
Suppose I have a char array that stores all the letters of the alphabet (say alpha) and I have an another char array (say letter) that contains some letters in it.
I want to retrieve those letters from the "letter" array and check its index in the "alpha" array and store that index into another integer array.
I have been trying this for quite some time now using charAt, indexOf but can't really get to implement it. Can someone help? Please.
I want to make a 2D array "data" with the following dimensions: data(T,N)
T is a constant and N I dont know anything about to begin with. Is it possible to do something like this in fortran
do i = 1, T
check a few flags
if (all flags ok)
c = c+ 1
data(i,c) = some value
end if
end do
Basically I have no idea about the second dimension. Depending on some flags, if those flags are fine, I want to keep adding more elements to the array.
How can I do this?
There are several possible solutions. You could make data an allocatable array and guess the maximum value for N. As long as you don't excess N, you keep adding data items. If a new item would exceed the array size, you create a temporary array, copy data to the temporary array, deallocate data and reallocate with a larger dimension.
Another design choice would be to use a linked list. This is more flexible in that the length is indefinite. You loss "random access" in that the list is chained rather than indexed. You create an user defined type that contains various data, e.g., scalers, arrays, whatever, and also a pointer. When you add a list item, the pointer points to that next item. The is possible in Fortran >=90 since pointers are supported.
I suggest searching the web or reading a book about these data structures.
Assuming what you wrote is more-or-less how your code really goes, then you assuredly do know one thing: N cannot be greater than T. You would not have to change your do-loop, but you will definitely need to initialize data before the loop.
i have a string array called Files, and a boolean function IsGood(Files[i]) in a for loop. How can i create an array of GoodFiles using IF.
Assuming I'm understanding you correctly, if you really must have an array you would first have to iterate over your Files array, calling IsGood on each one, and count how many good ones you have, allocate the array, then loop again, this time storing the good ones into the array.
There's another data structure, TStringList, however, that you can conveniently use for something like this:
GoodList := TStringList.Create;
for i := 0 to length(Files) - 1 do
if IsGood(Files[i]) then
GoodList.Add(Files[i]);