Find specic quantities within a vector or within a matrix in Matlab
I need to identify the time at which the following values occur, namely: eta = 0 and eta = 0:51;
I should work with 'find' code, but could not get desired value. I know how it works in 'maximum':
t = 10:1:10;
t = t';
eta = t .^2;
ind = find (eta == max(eta));
t_etamax = t(ind ,1);
t_etamax = t(ind ,1);
eta_max = eta (ind ,1);
Now I need not Maximum but when eta=0 and eta=0.51
See also questions close to this topic

Problem with separating Real and Imaginary parts for fmincon. How to program it correctly?
I am trying to solve this optimization problem with fmincon function in MATLAB: enter image description here
Where all H are complex matrices, g and Pdes are complex column vectors, D0 and E0 are numbers.
I expect to get complex column vector of g (and in general its should be complex), So I divided problem in two parts: real and imag, but it does not work, MATLAB returning me a message:
Not enough input arguments. Error in temp>nonlincon (line 17) c(1) = norm ( H_d*([g(1)+1i*g(4); g(2)+1i*g(5); g(3)+1i*g(6)]) )^2  D_0; Error in temp (line 12) = fmincon(objective,x0,[],[],[],[],[],[],nonlincon);
Where I am wrong?
And in general am I right in writing given problem in the following way:?
% For example: D_0 = 2*10^(5)*10^(60/10); E_0 = 50; H_b = rand(15,3) + 1i*rand(15,3); P_des = rand(15,1) + 1i*rand(15,1); H_d = rand(10,3) + 1i*rand(10,3); objective = @(g) (norm ( H_b*([g(1)+1i*g(4); g(2)+1i*g(5); g(3)+1i*g(6)])  P_des ))^2; x0 = ones(1,3*2)'; options = optimoptions('fmincon','MaxFunctionEvaluations',10e3); [X,FVAL,EXITFLAG,OUTPUT,LAMBDA,GRAD,HESSIAN]... = fmincon(objective,x0,[],[],[],[],[],[],nonlincon); % So I expect to get a column vector g: 1st 3 elements  Real part, next 3  Imag function [c,ceq] = nonlincon(g, H_d, E_0, D_0) c(1) = (norm ( H_d*([g(1)+1i*g(4); g(2)+1i*g(5); g(3)+1i*g(6)]) ))^2  D_0; c(2) = (norm ([g(1)+1i*g(4); g(2)+1i*g(5); g(3)+1i*g(6)]))^2  E_0; ceq = []; end

How to calculate weighted (grayscale) centroids of pixel clusters contained within bounding boxes on an image
I've written some code in MATLAB that converts an image (of stars) into a grayscale image and then into a binary image using a set threshold and then labels each cluster of pixels (stars) that is above this threshold. The labelling produces an output: e.g.
[1 1 1 0 0 0 0 0 0 1 1 0 0 0 2 2 2 0 0 0 0 3 3 0 2 0 0 0 0 0 3 3 0 0 0 0]
So each cluster of 1's, 2's, 3's etc. represents a star. After this the code then finds the centroids of each pixel cluster and draws a bounding box around each centroid (centered on the centroid) with an 8 x 8 pixel area.
I now want to take the coordinates of every pixel contained within each bounding box and perform a weighted centroiding algorithm by applying these coordinates to the grayscale image of the stars. So basically I want to find the weighted centroid of each bounding box that is produced.
Does anyone know how I would do this?
My code is shown below.
clc clear all close all img=imread('test.png'); % read image binary_image=convert2binary(img); % run function to convert image to binary imshow(binary_image); % open binary image %% Label each pixel cluster with a reference ID and store in output array 'B' visited = false(size(binary_image)); % initialise an array with same size as image array that is logical. This records which pixels have been visited. [rows, cols] = size(binary_image); B = zeros(rows, cols); % initialise an output array with all 0's that is the same size as the image array. Any 0's left don't belong to connected pixels. ID_counter = 1; % Labels connected pixels with unique ID's and keeps track of given ID's for row = 1:rows % search through rows of binary_image for col = 1:cols % search through columns of binary_image if binary_image(row, col) == 0 visited(row, col) = true; % if position == 0 mark as visited and continue elseif visited(row, col) continue; % if already visited position then continue else stack = [row col]; % if not visited before create stack with this location while ~isempty(stack) % while stack isn't empty loc = stack(1,:); % 1st row of stack stack(1,:) = []; % remove this location from stack if visited(loc(1),loc(2)) continue; % if stack location already visited then continue end visited(loc(1),loc(2)) = true; % is not visited before then mark as visited B(loc(1),loc(2)) = ID_counter; % mark this location in output array using unique ID [locs_y, locs_x] = meshgrid(loc(2)1:loc(2)+1, loc(1)1:loc(1)+1); % given this location, check 8 neighbouring pixels (N,E,S,W,NE,NW,SE,SW) locs_y = locs_y(:); locs_x = locs_x(:); out_of_bounds = locs_x < 1  locs_x > rows  locs_y < 1  locs_y > cols; % get rid of locations that are out of bounds of image locs_y(out_of_bounds) = []; locs_x(out_of_bounds) = []; is_visited = visited(sub2ind([rows cols], locs_x, locs_y)); % get rid of locations already visited locs_y(is_visited) = []; locs_x(is_visited) = []; is_1 = binary_image(sub2ind([rows cols], locs_x, locs_y)); % get rid of locations that are 0 locs_y(~is_1) = []; locs_x(~is_1) = []; stack = [stack; [locs_x locs_y]]; % add remaining locations to stack end ID_counter = ID_counter + 1; % ON THE LAST CLUSTER IT ADDS AN EXTRA 1 TO TOTAL, NOT WANTED! increase the unique ID by 1 after every cluster labelling end end end ID_counter = ID_counter  1; % REMOVES THE EXTRA 1 ADDED TO TOTAL ON THE LAST LOOP %% %% Calculate centroids of each labelled pixel cluster within binary image N = max(B(:)); % total number of pixel labels generated in output array sum_total = zeros(N,1); % create N x 1 array of 0's sum_yv = zeros(N,1); % " sum_xv = zeros(N,1); % " for xx=1:size(B,2) % search through y positions for yy=1:size(B,1) % search through x positions index = B(yy,xx); if index>0 sum_total(index) = sum_total(index) + 1; sum_yv(index) = sum_yv(index) + yy; sum_xv(index) = sum_xv(index) + xx; end end end centroids = [sum_xv, sum_yv] ./ sum_total; % calculates centroids for each cluster x_lower_limits = centroids(:,1)4; y_lower_limits = centroids(:,2)+4; % lower on image means larger y coord number x_upper_limits = centroids(:,1)+4; y_upper_limits = centroids(:,2)4; % higher on image means lower y coord number x_lower_limits(x_lower_limits<1)=1; % limit smallest x coord to image axis (1,y) y_lower_limits(y_lower_limits>size(binary_image,1))=size(binary_image,1); % limit largest y coord to image axis (x,517) x_upper_limits(x_upper_limits>size(binary_image,2))=size(binary_image,2); % limit largest x coord to image axis (508,y) y_upper_limits(y_upper_limits<1)=1; % limit smallest y coord to image axis (x,1) width = x_upper_limits(:,1)  x_lower_limits(:,1); % width of bounding box height = y_lower_limits(:,1)  y_upper_limits(:,1); % height of bounding box hold on for xl=1:size(x_lower_limits,1) r(xl)=rectangle('Position',[x_lower_limits(xl,1) y_upper_limits(xl,1) width(xl,1) height(xl,1)],'EdgeColor','r'); end for i=1:size(centroids,1) plot(centroids(i,1),centroids(i,2),'rx','MarkerSize',10) end hold off %% function [binary] = convert2binary(img) % Convert input image to grayscale and then binary image by thresholding [x, y, z]=size(img); % x=column, y=row if z>1 % if img is in colour img_gray=rgb2gray(img); % convert img to grayscale end % save('img_gray'); img_double=double(img_gray); % convert img to double precision % imshow(img_gray) sum=0; for i=1:x for j=1:y sum=sum+img_double(i, j); % total number of pixels in img end end threshold=100; % intensity threshold. sum/(x*y); binary=zeros(x,y); % create output array for binary img for i=1:x for j=1:y if img_gray(i, j) >= threshold % convert pixel to binary 'on' if intensity >= threshold binary(i, j) = 1; else binary(i, j)=0; % convert pixel to binary 'off' if intensity < threshold end end end end

Why vgg19 is very slow than alexnet
I'm trying to use pretrained model vgg19 and alexnet as feature extractor then using svm for classification, I have data about 15000 images. I'm using a server hp proliant g7 but vgg19 takes a lot of time in training which about 50h while alexnet takes about 4h. Is it normal?

Find: paths must precede expression  Running find on admin server
I am running command line to delete all file (except txt) older than 60 minutes.
I am running the following code on an admin server:
for server in {serA,serB,serC,serD,serE,serF}; do ssh root@$server find /var/webtemp/ type f ! name "*txt" mmin +60 delete; done
Which, for me, would have the same result as running the bellow code on each server:
find /var/webtemp/ type f ! name "*txt" mmin +60 delete
However when running the first command on the admin server I keep getting this error:
find: paths must precede expression: fundos_carteira_out20180321.txt Usage: find [H] [L] [P] [Olevel] [D helptreesearchstatratesoptexec] [path...] [expression]
Note I don't get this error when running the command on each server. Why this is happening?

Find a string into each file of a folder with for loop
I try to find a string "blabla" inside a file. If I find the string I replace it with "bloblo", if not do nothing. And I want to do this operation for all the files that I have in a folder.
When I apply the script on one file, it works well. When I use a for loop it doesn't work. it does not enter inside the for loop
Here is my script:
set "Var1=blabla" set "Var2=bloblo" FOR %%i IN (C:\Users\UserName\Desktop\TEST\*.txt) DO ( find /c "%Var1%" %%i if %errorlevel% equ 1 goto notfound call PathFile\FUNCTION_REPLACE.cmd %Var1% %Var2% %%i :notfound )
I use a script "Function_replace" to replace the values, here is the script:
SetLocal EnableExtensions DisableDelayedExpansion Set "search=%~1" Set "replace=%2" Set "File=%3" For /F "Tokens=1*Delims=]" %%A In ('Find /V /N ""^<"%File%"^&Break^>"%File%"' )Do (Set "line=%%B" SetLocal EnableDelayedExpansion (If Not "%%B"=="" (Echo(!line:%search%=%replace%!)Else Echo()>>"%File%" EndLocal)
I actually i used to use directly the call of my replace function for each files of my folder. But it takes a long time to go inside each file. That's why I want to check before going inside each file, if the string exists or not (by using find /c).
Can you spot where is the issue coming from?

Find and Remove function call with brackets
I'm working on a python script that will allow me to remove some attributes from a function call in a Java class. The problem is I can't find the right regex to include both the name of the attribute and the brackets.
The string I'm looking to remove is, as an example, 'withContentDescription("random text")'
What is the correct way to include the () brackets and the random content of those within my code? import re
filein = '/path/file.java' fileout = '/path/newfile.java' f = open(filein,'r') filedata = f.read() f.close() print("Removing Content Descriptor") newdata = filedata.strip("withContentDescription\)") f = open(fileout,'w') f.write(newdata) print(" Done") f.close()
I'd like to obtain something like
old string: allOf(withId(someinfo), withContentDescription("Text")) new string: allOf(withId(someinfo))