selecting to do operation on every fourth row

2 次查看(过去 30 天)
this quetion might sound confusing
i have the 840 rows of the following is a sample
f(:,1) f(:,2) f(:,3)
air 0.693 0.329 0.013
train 0.109 0.203 0.168
bus 0.356 0.153 0.250
car 0.010 0.076 0.193
this the previous function i had which was easy
fun = @(x)[(((1+x(4))*(x(1)*f(:,1) + x(2)*f(:,2) + x(3)*f(:,3))) + (x(4)*((f(:,1).^x(1)).*(f(:,2).^x(2)).*(f(:,3).^x(3)))) + ep - en - u);
the question i like to code the following for all the my data. i want to do the following opation on every 4 rows grouped togther
to explain further
x(1)*((column1,row1) - (column1,row2))+ x(2)*((column2,row1)-(column2,row2))+ x(3)*((column3,row1)-(column3,row2))
x(1)*((column1,row1) - (column1,row3))+ x(2)*((column2,row1)-(column2,row3))+ x(3)*((column3,row1)-(column3,row3))
x(1)*((column1,row1) - (column1,row4))+ x(2)*((column2,row1)-(column2,row4))+ x(3)*((column3,row1)-(column3,row4))
i am basically taking the first row minus the other rows in each column
this will repeat for the set of 4 untill i get to the end of 840 enteries
i hope someone can help

采纳的回答

Samatha Aleti
Samatha Aleti 2020-5-5
Hi,
As per my understanding you want to consider every 4 rows as 1 set , apply your formula and repeat this till the last row. You can use a vector as index with increments of 4 and define the logic (for formula) using this index” accordingly. Here is a sample code:
f = randi(5,[840 3]); % Consider random array of size 840 x 3
y = zeros(840,1); % Array to store output
i= 1: 4: 840; % Index
y(i) = (f(i,1) - f(i+1,1)) + (f(i,2) - f(i+1,2)) + (f(i,3) - f(i+1,3));
y(i+1) = (f(i,1) - f(i+2,1)) + (f(i,2) - f(i+2,2)) + (f(i,3) - f(i+2,3));
y(i+2) = (f(i,1) - f(i+3,1)) + (f(i,2) - f(i+3,2)) + (f(i,3) - f(i+3,3));
y(i+3) = NaN; % Ignore
  1 个评论
Abdulaziz Altowijri
i think this is similar to what i am looking for is it easy to adapt this to my code
%% Optimization - Minimizing Sum of Squared Errors
% Minimum SSE = sum(ep)^2 + sum(en)^2
% Clearing Command Window
clc
% Clearing Variables from Workspace
clearvars
close all
% Reading Data from Excel File & Sotring in variable "f"
f = xlsread('data.xlsx');
% Finding Size of Data
[m, n] = size(f);
% % Taking Input from user for values of U
% fprintf('\n\tEnter Value of U\t:\t')
% u = input('');
% Defining Lower Limits of [beta alpha w(1) w(2) w(3)]
% beta > 0
% alpha (Unrestricted in sign)
% w(i) >= 0
lb = [0.01 -inf 0 0 0];
% Defining Upper Limits of [beta alpha w(1) w(2) w(3)]
% beta < infinity
% alpha (Unrestricted in sign)
% w(i) <= 1
ub = [inf inf 1 1 1];
%Initial values of ep & en
ep=abs(0.1*rand(840,1));
en= abs(0.1*rand(840,1));
% Guessing Initial Values of [beta alpha w(1) w(2) w(3)]
x0 = [1e-1 0 0.6 0.2 0.2];
u=0.25*ones(840,1);
% Defining Function for optimization
%% here is where i need to use the index part here i am using to work with the the lsqnonlin function
fun = @(x)[x(3)*f(:,1).^x(1) + x(4)*f(:,2).^x(1) + x(5)*f(:,3).^x(1)+ep-en - x(2)*u;
x(3) + x(4) + x(5)-1];
[x,SSE] = lsqnonlin(fun,x0,lb,ub,options);

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

产品


版本

R2019b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by