Matlab: select a portion of a matrix
The following script gives me the error "Subscript indices must either be real positive integers or logicals."
I do not understand why, since the vars are integers. Moreover, I also cast them to integers.
The code is:
x_start = int16(xmatrixRay);
x_end = uint16(x+matrixRay);
y_start = uint16(ymatrixRay);
y_end = uint16(y+matrixRay);
matrix = img(x_start:x_end,y_start:y_end);
I put a breakpoint just before the code above. Below I list the values of the variables:
K>> x_start
x_start =
uint16
298
K>> x_end
x_end =
uint16
302
K>> y_start
y_start =
uint16
88
K>> y_end
y_end =
uint16
92
K>> matrixRay
matrixRay =
2
Clearly I cannot post all the matrix "img", since it is pretty big. The workspace it is a 1369x1173 uint8.
1 answer

As others have pointed out, int8 (128 to 127) clearly won't cover your image size. Given the size, you may use uint16 for index. It doesn't make much sense to use signed integer for indexing. You may simply use round to create index in case it could be noninteger.
But int8 just gives you wrong result (worse than error in my opinion). The error you got is because your x_start or y_start is zero. You can check this easily by setting up a breakpoint.
If you are sure what you are doing is what you want, you may fix the problem like this:
x_start = round(xmatrixRay); % or uint16 if size is less than 2^16 x_start = max(x_start, 1); % use 1 if less than 1 x_end = round(x+matrixRay); x_end = min(x_end, size(img,1)); % clip to max of img size
The similar should be applied to y_start and y_end.
See also questions close to this topic

Passing variable sized arguments to a method
I'm using the PrintTable method of the matlabtools. In the documentation the following example is given:
t = PrintTable('LaTeX/PDF export demo, %s',datestr(now)); t.HasRowHeader = true; t.HasHeader = true; t.addRow('A','B','C');
Let's say I have the header names in a cell array
header = {'A','B','C'}
. Unfortunately a cell array cannot be passed toaddRow
, i.e.t.addRow(header);
does not work. Unfortunately the size of the header varies in my case, that's why I'm storing it in a cell array.How can I call the
addRow
method with a variable sized cell array? 
MatCaffe  How to run 2 Caffe nets on different GPUs?
So in C++, I am parallelizing over the number of GPUs my net. I.e. if I receive 2 images, I process in 2 different threads at the same time: image 1 with a Caffe net opened on GPU 0; and image 2 with another Caffe net opened on GPU 1.
I am trying to do the same with MatCaffe, but I am getting this error:
F1023 14:39:07.943541 2780 cudnn_conv_layer.cu:28] Check failed: status == CUDNN_STATUS_SUCCESS (8 vs. 0) CUDNN_STATUS_EXECUTION_FAILED *** Check failure stack trace: ***
My code is something like this:
% Initialization numberGpuStart = 0; net = cell(numberGpus, 1); caffe.reset_all(); for gpuId = 1:numberGpus caffe.set_mode_gpu(); caffe.set_device(gpuId1+numberGpuStart); net{gpuId} = caffe.Net(model.deployFile, model.caffemodel, 'test'); end % Other code... % Processing each image for imageId = 1:numberGpus:numberImages for coreId = 0:numberGpus1 % inputData made from image{imageId+coreId} net{coreId}.forward(inputData) end end
Assume that
numberImages
can be perfectly divided intonumberGpus
(so there is no leftovers that lead to runtime crash, I can handle that). And I am not usingparfor
inparfor coreId = 0:numberGpus1
to debug the error first, I'd add it at then end to truly have 4 parallelized threads.If I run this with 1 GPU, i.e. setting
numberGpus = 1
andnumberGpuStart
to any number 03, it works (independently of using GPU 0,1,2 or 3). If I tried 2 at the same time, it crashes.And extra thing that worked, if I change
net{coreId}.forward(inputData)
bynet{2}.forward(inputData)
(or any other number in the valid range), then it works. This means that the problem is not initializing the nets, but using more than 1.Any idea what else I must do? Thanks!
(PS: the code is very big to be completely copied here, and needs lot of auxiliar files, I just added the basic idea to know if someone has experience with this. Please ask any extra detail you might need!)

Matlab plot a base map under a 3D volume rendering result
I use function
pcolor3
to draw a volume rendering result like the following one. And I want to plot a base map:The question is I can do the two images separately. But when I combine them together, both of them are missing:
Can anyone help me find a solution? Thanks.

Copy rows into python dataframe using column logical indexing
Hello fellow programmers,
I am trying to copy rows into a dataframe using logical indexing but I get nans. The idea is to quickly replace a number of rows in a dataframe using logical indexing:
Example how I expected it to work:
# pseudo code: example to create logical vector logical_vector = df.loc[:,'colname']==x # pseudo code: example to use logical vector to index a dataframe df[logical_vector,:]=df1.loc[logical_vector2,:]
This is one of the basic operations that, if possible, allows quick matrix operations. How to solve this, preferrably without a loop?
I have created an example to show what the problem is:
# create example 10x5 dataframe containing random numbers x=pd.DataFrame(np.random.randn(10,5),columns=['ac', 'bc','cc','dc', 'ec'],index = ['a','b','c','d','e','f','g','h','i','j']) # add a column containing information for use in example of logical indexing x['t']= [1,0,1,0,1,0,1,0,1,0] x Out[76]: ac bc cc dc ec t a 1.029517 1.936904 1.143655 0.708996 1.218484 1 b 1.836638 0.723243 0.501546 2.046355 0.248156 0 c 2.369828 0.559880 0.878904 0.673454 0.630927 1 d 0.629210 1.261608 0.190508 0.582700 0.068166 0 e 1.500134 0.534379 0.375362 0.849761 1.675824 1 f 1.399520 0.038366 0.137986 0.156580 0.674619 0 g 1.359863 0.433721 0.625973 0.477530 0.542612 1 h 0.694573 0.196907 0.372210 0.464188 1.217399 0 i 1.357809 0.017611 0.539137 1.016894 0.172672 1 j 0.366195 0.750404 0.055895 0.358795 0.181593 0
Then I try to replace using indexing I get this:
# Use logical indexes x.loc[:,'t']==0 and x.loc[:,'t']==1 to point and get # data into x. This should replace all row values that contain '0' in column # t with row values from columns that have '1' for column t x.loc[x.loc[:,'t']==0,:]=x.loc[x.loc[:,'t']==1,:] x Out[78]: ac bc cc dc ec t a 1.029517 1.936904 1.143655 0.708996 1.218484 1.0 b NaN NaN NaN NaN NaN NaN c 2.369828 0.559880 0.878904 0.673454 0.630927 1.0 d NaN NaN NaN NaN NaN NaN e 1.500134 0.534379 0.375362 0.849761 1.675824 1.0 f NaN NaN NaN NaN NaN NaN g 1.359863 0.433721 0.625973 0.477530 0.542612 1.0 h NaN NaN NaN NaN NaN NaN i 1.357809 0.017611 0.539137 1.016894 0.172672 1.0 j NaN NaN NaN NaN NaN NaN
While I was expecting this:
Out[76]: ac bc cc dc ec t a 1.029517 1.936904 1.143655 0.708996 1.218484 1 b 1.029517 1.936904 1.143655 0.708996 1.218484 1 c 2.369828 0.559880 0.878904 0.673454 0.630927 1 d 2.369828 0.559880 0.878904 0.673454 0.630927 1 e 1.500134 0.534379 0.375362 0.849761 1.675824 1 f 1.500134 0.534379 0.375362 0.849761 1.675824 1 g 1.359863 0.433721 0.625973 0.477530 0.542612 1 h 1.359863 0.433721 0.625973 0.477530 0.542612 1 i 1.357809 0.017611 0.539137 1.016894 0.172672 1 j 1.357809 0.017611 0.539137 1.016894 0.172672 1
Did I miss something?

R add incremental row id to data frame when row value changes
I can determine the index of the row when the change occurs using the lag function, example:
idx < which(as.character(id) != lag(as.character(id)))
But I cannot work out how to apply this to a new row so that the index will increment at each change point
Input
c1 apple apple pear pear apple
Wrong Output
id c1 1 apple 1 apple 2 pear 1 pear 1 apple
Desired Output
id c1 1 apple 1 apple 2 pear 2 pear 3 apple

Indexing one PyTorch tensor by another using index_select
I have a 3 x 3 PyTorch LongTensor that looks something like this:
A = [0, 0, 0] [1, 2, 2] [1, 2, 3]
I want to us it to index a 4 x 2 FloatTensor like this one:
B = [0.4, 0.5] [1.2, 1.4] [0.8, 1.9] [2.4, 2.9]
My intended output is the 2 x 3 x 3 FloatTensor below:
C[0,:,:] = [0.4, 0.4, 0.4] [1.2, 0.8, 0.8] [1.2, 0.8, 2.4] C[1,:,:] = [0.5, 0.5, 0.5] [1.4, 1.9, 1.9] [1.4, 1.9, 2.9]
In other words, matrix
A
is indexing and broadcasting matrixB
.A
is the matrix of indices ofB
, so this operation is essentially an indexing operation.How can this be done using the
torch.index_select()
function? If the solution involves adding or permuting dimensions, that's fine. 
Java generictyped method returns a wrong type
Every once in a while, I feel I don't know Java at all.. I recently found this strange behavior of Java type casting:
public static void main(String[] args) { String res = get(); System.out.println(res); } public static <T> T get() { Object longObj = Long.valueOf("0"); T casted = (T) longObj; System.out.println("longObj=" + longObj.getClass()); System.out.println("casted=" + casted.getClass()); // < Why the type of "casted" is Long instead of String??? return casted; }
The output is:
longObj=class java.lang.Long casted=class java.lang.Long Exception in thread "main" java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.String at com.apple.geo.coreloc.Test.main(Test.java:5)
My confusion is: Why variable
casted
has type ofLong
? Shouldn't it be casted to typeT
, that isString
?Following is why I thought it should be
String
: At runtime,
String res = get()
hints get() method to return a String T casted = (T) longObj;
should try to type cast Long to a String (I expect an exception here, but there isn't..)
 At runtime,

Math.random Problems
I'm trying to make a program where Math.random() generated a list of random numbers as ints between the range of 3 and 9. I keep getting long decimals and numbers such as 0, or 1. I'm not sure if it's the output() or is my process right? Or is it something simple like typecasting?
Here's the output i'm getting with this code:
Configuration: <Default> 5.855672310088313 5.0287286763569625 5.1257975957882485 3.2283987551436173 0.8364642553235297 3.5005965809506696 3.284959203422762 1.3896135045374878 2.8493705949526733 0.7431149270852646 Process completed.
Im trying to get something more along the lines of a list of 10 random numbers between 3 and 9.
import static java.lang.System.*; import java.util.*; public class Java1605{ public static void main(String[] args){ new Problem(); }} class Problem { int low; int high; int range; public Problem() { low = 3; high = 9; range = (int)((Math.random() * (high  low )) + low); output(); } public void output() { out.println(Math.random()* range); out.println(Math.random()* range); out.println(Math.random()* range); out.println(Math.random()* range); out.println(Math.random()* range); out.println(Math.random()* range); out.println(Math.random()* range); out.println(Math.random()* range); out.println(Math.random()* range); out.println(Math.random()* range); } }

Convert ArrayList object to String
I have an
ArrayList
of typeObject
. I choseObject
as datatype as I intend to store bothString
andinteger
in the list.ArrayList<Object> data = new ArrayList<Object>();
I've populated the
ArrayList
from aResultSet
object which is populated from the data I got from database.data.add(result.getInt("id")); data.add(result.getString("username"));
I want to store the id value in a cookie. That's why I tried to convert the first data from the list, which is an
int
, to aString
.So I did the following steps, but it's causing my program to break.Cookie ck = new Cookie("id",(String) data.get(0));
How can I solve this issue?

C# program as a console application that allows a user to input any no of positive integers between 1 and 100
Hi I'm fairly new to C# & I am trying to complete the following questions. Any help at all would be much appreciated. I'm trying to create a "do while" loop& if statements in order to run the program.
Write a C# program as a console application that allows a user to input any no of positive integers between 1 and 100. When the user enters any number outside the given range, the program displays the following: a) No of even numbers entered. b) Sum of even numbers entered. c) Average of even numbers entered (Average must be a real number formatted to display 2 decimal places). d) Highest and lowest even numbers
//Declare variables int inputValue; int odd = 0; int sum = 0; int evenCount = 0; int high = 0; int low = 0; double average; do { for (int number = 0; evenCount < 5; number++) { if (number % 2 == 0)//check if even { sum = sum + number; //add to sum evenCount++;//increment evenCount } number++;//increment number //Display sum Console.WriteLine($"Sum of first five even numbers is: {sum}"); } } } }
}

Python 3.x: Trying to compare current element with previous and next (without builtin functions)
I'm trying to write code that adds to a count if the current list element is greater than both the previous list element and the next list element. So, for example, in a list: [1, 3, 5, 7, 2]... 1 and 2 are not evaluated because they only have one number to the side. So 3 is the first to be compared and 3 is not greater than 1 and 5, 5 is not greater than 3 and 7, and 7 is greater than 5 and 2 so count = 1.
This is for homework so I can't use builtin functions. List is of random length and may contain positive and negative numbers.
I tried two variations... In this version, the i value never goes up. Why is this? How do I get it to go to the next number?
a = [int(i) for i in input().split()] b = a[0] count = 0 for i in a[1:]: for j in a[2:]: if i > j and i > b: count +=1 b = i print(count)
In the second version, I get Index Error: list index out of range. How can I fix such an error?
a = [int(i) for i in input().split()] prev = nxt = 0 b = a[1] count = 0 for i in range(1, len(a)): prev = a[i1] nxt = a[i+1] if b > prev and b > nxt: count +=1 prev = b b = nxt print(count)
Thank you for any help. I would like to know where I'm going wrong.

How to write a LessThan method without using the operator
How would you recursively write a method that checks if a number is less than the other without using the '<' operator?
 You can only use the plus, minus, times, and equals operators.
 It must be recursive
x
andy
will always be 0 or greater Should return
boolean
 If needed, you can make other methods but they must follow rules above.
Cove I've got so far:
public static boolean isLessThan(int x, int y) { if(x == y  1) return true; if(x == y + 1) return false; if(x == y) return false; return isLessThan((x), (y1))  isLessThan((x1), y); }