Answered

Attempting to automate the importing of text file data

%Let directory 'data' have your files: 40_8deg_HL_Both.txt and 40_4deg_HL_Both.txt n = dir('path_to_data/data/*Both.txt'); m =...

Attempting to automate the importing of text file data

%Let directory 'data' have your files: 40_8deg_HL_Both.txt and 40_4deg_HL_Both.txt n = dir('path_to_data/data/*Both.txt'); m =...

1 day ago | 0

| accepted

Answered

How can I convert cell contents to a matrix?

T = readtable('40_8deg_HL_Both.txt','ReadVariableNames',false,... 'Format','%{yyyyMMdd hh:mm:ss.SSS}D %f %f %f %f %f %...

How can I convert cell contents to a matrix?

T = readtable('40_8deg_HL_Both.txt','ReadVariableNames',false,... 'Format','%{yyyyMMdd hh:mm:ss.SSS}D %f %f %f %f %f %...

2 days ago | 1

| accepted

Answered

How can i store values calculated in my loop

X = [9 20 15 9 10 5;5 20 5 20 15 3;15 29 39 49 5 10]; [rows, columns]=size(X); for r=1:columns for k=1:3 if X(k,...

How can i store values calculated in my loop

X = [9 20 15 9 10 5;5 20 5 20 15 3;15 29 39 49 5 10]; [rows, columns]=size(X); for r=1:columns for k=1:3 if X(k,...

3 days ago | 1

Answered

count the quantity of prime number before my number

x=input('Input a positive integer, or 0 to quit: '); z = [2,3:2:x]; y= mod(z(:),z(:)'); out = z(all(tril(y) + triu(ones(siz...

count the quantity of prime number before my number

x=input('Input a positive integer, or 0 to quit: '); z = [2,3:2:x]; y= mod(z(:),z(:)'); out = z(all(tril(y) + triu(ones(siz...

3 days ago | 0

Answered

How to create a matrix using conditional statements in a loop based in another matrix

H = all(Comb,2).*sum(Comb,2);

How to create a matrix using conditional statements in a loop based in another matrix

H = all(Comb,2).*sum(Comb,2);

3 days ago | 1

| accepted

Answered

Find the first element that satisfy a condition in a vector

function [summa, index] = max_sum(A,n) B = movsum(A(:),[0 n-1]); summa = max(B); index = find(summa == B); end ...

Find the first element that satisfy a condition in a vector

function [summa, index] = max_sum(A,n) B = movsum(A(:),[0 n-1]); summa = max(B); index = find(summa == B); end ...

3 days ago | 0

| accepted

Answered

How to plot matrix inside for loop ?

A = [0 3/10;3/5 0]; u = [0.3 ;0.8]; p = [0;0]; P = zeros(2,50); for n = 1:50 p = A*p + u; P(:,n) = p; end plo...

How to plot matrix inside for loop ?

A = [0 3/10;3/5 0]; u = [0.3 ;0.8]; p = [0;0]; P = zeros(2,50); for n = 1:50 p = A*p + u; P(:,n) = p; end plo...

3 days ago | 1

Answered

How to group an array and then replace each group with different numbers?

B = A; B(A==0) = repelem(C,diff(reshape(find(diff([1,A,1]) ~= 0),2,[])));

How to group an array and then replace each group with different numbers?

B = A; B(A==0) = repelem(C,diff(reshape(find(diff([1,A,1]) ~= 0),2,[])));

3 days ago | 1

| accepted

Answered

Center of gravity between 2 points

x = [1 3]; y = [2 5]; v = [10 20]; xc = sum(v.*x)/sum(v); yc = sum(v.*y)/sum(v);

Center of gravity between 2 points

x = [1 3]; y = [2 5]; v = [10 20]; xc = sum(v.*x)/sum(v); yc = sum(v.*y)/sum(v);

4 days ago | 0

| accepted

Answered

How to replace some values in an array with zero and some with values from another array?

A = [2 4 0 0 5 3 0 2 1 0 2]; B = [1 1 0 1]; A1 = ~A; C = double(A1); C(A1) = B;

How to replace some values in an array with zero and some with values from another array?

A = [2 4 0 0 5 3 0 2 1 0 2]; B = [1 1 0 1]; A1 = ~A; C = double(A1); C(A1) = B;

4 days ago | 0

Answered

Combining vectors [1 2 3 4 5] and [2 3 4 1 5] to [12 23 34 41 55]

A = [1 200 3 4 5]; B = [23 3 445 10 54]; out = str2double(string(A)+B); or out = A.*10.^(floor(log10(B))+1) + B;

Combining vectors [1 2 3 4 5] and [2 3 4 1 5] to [12 23 34 41 55]

A = [1 200 3 4 5]; B = [23 3 445 10 54]; out = str2double(string(A)+B); or out = A.*10.^(floor(log10(B))+1) + B;

4 days ago | 0

Answered

Vectorize a for loop

Solution for attached data: [~,sheets] = xlsfinfo('MapList.xlsx'); n = numel(sheets); ml = cell(n,1); for ii = 1:n ml{i...

Vectorize a for loop

Solution for attached data: [~,sheets] = xlsfinfo('MapList.xlsx'); n = numel(sheets); ml = cell(n,1); for ii = 1:n ml{i...

4 days ago | 0

Answered

How to make the main diagonal and the diagonal above and below the main diagonal equal to -2 and 1 respectively

A = gallery('tridiag',100,-1,2,-1); or n = 100; A = spdiags(ones(n,1)*[-1 2 -1],-1:1,n,n);

How to make the main diagonal and the diagonal above and below the main diagonal equal to -2 and 1 respectively

A = gallery('tridiag',100,-1,2,-1); or n = 100; A = spdiags(ones(n,1)*[-1 2 -1],-1:1,n,n);

4 days ago | 1

Answered

Vectorize a for loop

Please attach your data (bimage,map.xlsx - small example) as mat-file and xlsx-file. ml = zeros(200,2,66); for ii = 1:size(ml,...

Vectorize a for loop

Please attach your data (bimage,map.xlsx - small example) as mat-file and xlsx-file. ml = zeros(200,2,66); for ii = 1:size(ml,...

4 days ago | 0

Answered

how to make a column vector with evenly spaced entries with different coefficients

k = -4:-1; y = 3; n = numel(k); y = [zeros(n-1,n);y*k(:)']; ycoeff = y(:); or k = -4:-1; y = 3; n = numel(k); ycoe...

how to make a column vector with evenly spaced entries with different coefficients

k = -4:-1; y = 3; n = numel(k); y = [zeros(n-1,n);y*k(:)']; ycoeff = y(:); or k = -4:-1; y = 3; n = numel(k); ycoe...

8 days ago | 0

| accepted

Answered

Convert an array of numbers into letters using logical indexing

v = [0, 5:.5:10]'; S = string(cellstr(('F':-1:'A')')); K = S(4:6)' + {'-','','+'}'; K = [S(1:3);K(1:end-1)']; out = discret...

Convert an array of numbers into letters using logical indexing

v = [0, 5:.5:10]'; S = string(cellstr(('F':-1:'A')')); K = S(4:6)' + {'-','','+'}'; K = [S(1:3);K(1:end-1)']; out = discret...

8 days ago | 0

Answered

How do I compute 3 minute moving average in timeseries?

Let ts - your timeseries object. k = 3*60/2; % the number of elements corresponding to three minutes in your case ts.Data(:,2)...

How do I compute 3 minute moving average in timeseries?

Let ts - your timeseries object. k = 3*60/2; % the number of elements corresponding to three minutes in your case ts.Data(:,2)...

9 days ago | 0

Answered

I have a huge matrix to A to solve for Ax=B. There are some rows with zero elements in A and I want to modify the diagonal for these rows. So how can I fast find these rows with all zero elements? for loop is rather slow.

"How to set diagonal as 1 for the row with all zero elements?" A = A + diag(~diag(A)); or n = all(A==0,2); A = A + diag(n); ...

I have a huge matrix to A to solve for Ax=B. There are some rows with zero elements in A and I want to modify the diagonal for these rows. So how can I fast find these rows with all zero elements? for loop is rather slow.

"How to set diagonal as 1 for the row with all zero elements?" A = A + diag(~diag(A)); or n = all(A==0,2); A = A + diag(n); ...

10 days ago | 0

Answered

Convert hourly to daily data

T = readtable('IT_GR_data.csv','ReadV',1); T.Properties.VariableNames{1} = 'Date'; TT = table2timetable(T,'RowTime','Date');...

Convert hourly to daily data

T = readtable('IT_GR_data.csv','ReadV',1); T.Properties.VariableNames{1} = 'Date'; TT = table2timetable(T,'RowTime','Date');...

10 days ago | 0

| accepted

Answered

How to solve this loop equation

data = readtable('data.xlsx','ReadV',0); data.Properties.VariableNames = {'a','b'}; m = 0:.01:1; [ii,idx] = ndgrid(m,1:heig...

How to solve this loop equation

data = readtable('data.xlsx','ReadV',0); data.Properties.VariableNames = {'a','b'}; m = 0:.01:1; [ii,idx] = ndgrid(m,1:heig...

10 days ago | 1

| accepted

Answered

Values of vectors in matrix (changes in time)

t =0:pi/20:4*pi; x =reshape(cos((1:4)'*t),2,2,[]);

Values of vectors in matrix (changes in time)

t =0:pi/20:4*pi; x =reshape(cos((1:4)'*t),2,2,[]);

12 days ago | 1

Answered

I need help with for loops with if statements. I'm trying to get it to output [P,O,O,O,P,O,P,U,U,O,O,O,U,O,O,A,A,A,P,P,G,O,U,U,G,A,P,O]. thx

volt=[18 33 31 34 15 37 10.5 48 50 38 35 39 42 33 31 1 5 9 13 11 27 35 -1 46 22 6 19 36]; ABS = sprintfc('%c','UAPGOU'); [~,...

I need help with for loops with if statements. I'm trying to get it to output [P,O,O,O,P,O,P,U,U,O,O,O,U,O,O,A,A,A,P,P,G,O,U,U,G,A,P,O]. thx

volt=[18 33 31 34 15 37 10.5 48 50 38 35 39 42 33 31 1 5 9 13 11 27 35 -1 46 22 6 19 36]; ABS = sprintfc('%c','UAPGOU'); [~,...

12 days ago | 1

Answered

find frequency and duration of numerical events in a matrix

Let A - your array (1440 x Days): k = 60;% k = 60 (minuties in hour) [m,n] = size(A); a1 = A ~= 0; A1 = diff([false(1,n);a1]...

find frequency and duration of numerical events in a matrix

Let A - your array (1440 x Days): k = 60;% k = 60 (minuties in hour) [m,n] = size(A); a1 = A ~= 0; A1 = diff([false(1,n);a1]...

16 days ago | 0

| accepted

Answered

How to take 3 elements at a time from an array and turn them into their own column vector?

msg = 'gfewqaxcvbhgf'; % example [~,C] = ismember(msg,'a':'z'); C = C - 1; out = reshape([C(:);zeros(mod(-numel(C),3),1)],3...

How to take 3 elements at a time from an array and turn them into their own column vector?

msg = 'gfewqaxcvbhgf'; % example [~,C] = ismember(msg,'a':'z'); C = C - 1; out = reshape([C(:);zeros(mod(-numel(C),3),1)],3...

16 days ago | 1

| accepted

Answered

How to use 'for' in this example?

a = [y11(:)';y22(:)';y33(:)';y44(:)';y55(:)';y66(:)']; aaa = reshape(repelem(a,1,2),1,[]);

How to use 'for' in this example?

a = [y11(:)';y22(:)';y33(:)';y44(:)';y55(:)';y66(:)']; aaa = reshape(repelem(a,1,2),1,[]);

18 days ago | 0

| accepted

Answered

Finding the first and the last elements of consecutive numbers and combining them with a semi colon.

ii = cumsum([true,diff(outliers)~=1]); A = accumarray(ii(:),outliers(:),[],@(x){[min(x),max(x)]}); out = reshape([A{:}],2,[])'...

Finding the first and the last elements of consecutive numbers and combining them with a semi colon.

ii = cumsum([true,diff(outliers)~=1]); A = accumarray(ii(:),outliers(:),[],@(x){[min(x),max(x)]}); out = reshape([A{:}],2,[])'...

18 days ago | 1

| accepted

Answered

Interating through a table data set

sex = {'F';'M'}; age = [55;40]; [~,~,ii] = histcounts(age,agetable.agelow); [lo,jj] = ismember(sex,{'F','M'}); at = agetab...

Interating through a table data set

sex = {'F';'M'}; age = [55;40]; [~,~,ii] = histcounts(age,agetable.agelow); [lo,jj] = ismember(sex,{'F','M'}); at = agetab...

18 days ago | 0

| accepted

Answered

Only the value corresponding to the last loop of a for loop being saved in the output array

c = 12000; k = (1:20)'; soc = k*c/numel(k); lo = ismember(round(data(:,16)),soc); ocv = data(lo,14);

Only the value corresponding to the last loop of a for loop being saved in the output array

c = 12000; k = (1:20)'; soc = k*c/numel(k); lo = ismember(round(data(:,16)),soc); ocv = data(lo,14);

18 days ago | 1

| accepted

Answered

How to randomly interchange values in different rows in a matrix

[m,n] = size(A); At = A.'; [~,ii] = sort(rand([n,m])); cutpoint = find(ii == 1); i0 = zeros([n,m]); i0(1,:) = 1; i0(ccutpo...

How to randomly interchange values in different rows in a matrix

[m,n] = size(A); At = A.'; [~,ii] = sort(rand([n,m])); cutpoint = find(ii == 1); i0 = zeros([n,m]); i0(1,:) = 1; i0(ccutpo...

18 days ago | 0