divide and conquer
조회 수: 4 (최근 30일)
이전 댓글 표시
please
anyone can give me source code about divide and conquer method
thank you
댓글 수: 2
Oleg Komarov
2012년 3월 25일
Being "an algorithm design paradigm" there's no specific source code for it: http://en.wikipedia.org/wiki/Divide_and_conquer_algorithm
Also, additional info could give contributors a chance of answering:
http://www.mathworks.com/matlabcentral/answers/6200-tutorial-how-to-ask-a-question-on-answers-and-get-a-fast-answer
답변 (3개)
Walter Roberson
2012년 3월 25일
Certainly. In order for this to be a proper "divide and conquer" source contribution, each of the readers of this site will contribute part of the source, and it will be up to you to put everything together.
My contribution to your D&Q source follows on the next line:
=
Hope that helps!
댓글 수: 4
Walter Roberson
2017년 5월 22일
Peter Weigel comments to Walter Roberson:
This comment is detrimental to the health of the MathWorks forums.
Walter Roberson
2017년 5월 22일
Peter Weigel: the MathWorks forums seem to have thrived in the five years since I posted that response to someone who asked a vague question and expected to be given full source without any effort on their part.
RAJESH
2024년 8월 29일
clc; clear all; close all;
% Initialization
iter = 1;
x = [0 0.25 0.5 0.75 1];
y = [5 6 3 2 8];
% Prepare X1 and Y1 matrices
X1 = zeros(4,4);
x1 = [x(3) x(4) x(5)];
x2 = [x(3) x(4) x(5)];
x3 = [x(1) x(2) x(3)];
x4 = [x(1) x(2) x(3) x(4)];
r = [length(x1) length(x2) length(x3) length(x4)];
X1(1,1:r(1)) = x1;
X1(2,1:r(2)) = x2;
X1(3,1:r(3)) = x3;
X1(4,1:r(4)) = x4;
Y1 = zeros(4,4);
y1 = [y(3) y(4) y(5)];
y2 = [y(3) y(4) y(5)];
y3 = [y(1) y(2) y(3)];
y4 = [y(1) y(2) y(3) y(4)];
r = [length(y1) length(y2) length(y3) length(y4)];
Y1(1,1:r(1)) = y1;
Y1(2,1:r(2)) = y2;
Y1(3,1:r(3)) = y3;
Y1(4,1:r(4)) = y4;
alpha = [-0.8 0.7 0.7 -0.8];
N = length(x);
% Initialize d and d1
d = zeros(1, N-1); % Adjust the size as needed
d1 = zeros(4, 4); % Adjust the size as needed
% Computation of a, b, and q values
for i = 1:N-1
a(i) = (x(i+1) - x(i)) / (X1(i, r(i)) - X1(i, 1));
b(i) = (X1(i, r(i)) * x(i) - X1(i, 1) * x(i+1)) / (X1(i, r(i)) - X1(i, 1));
% Calculating q(i) with careful attention to parentheses
term1 = (y(i) - alpha(i) * Y1(i, 1)) * (1 - (x(i) - X1(i, 1)) / (X1(i, r(i)) - X1(i, 1))).^3;
term2 = (y(i+1) - alpha(i) * Y1(i, r(i))) * ((x(i) - X1(i, 1)) / (X1(i, r(i)) - X1(i, 1))).^3;
term3 = (r(i) * (y(i) - alpha(i) * Y1(i, 1)) + (x(i+1) - x(i)) * d(i) - alpha(i) * d1(i, 1) * (X1(i, r(i)) - X1(i, 1))) * ...
(1 - (x(i) - X1(i, 1)) / (X1(i, r(i)) - X1(i, 1))).^2 * (x(i) - X1(i, 1)) / (X1(i, r(i)) - X1(i, 1));
term4 = (r(i) * (y(i+1) - alpha(i) * Y1(i, r(i))) - (x(i+1) - x(i)) * d(i+1) + alpha(i) * d1(i, r(i)) * (x1(i, r(i)) - x1(i, 1))) * ...
(1 - (x(i) - X1(i, 1)) / (X1(i, r(i)) - X1(i, 1))) * ((x(i) - X1(i, 1)) / (X1(i, r(i)) - X1(i, 1))).^2;
numerator = term1 + term2 + term3 + term4;
denominator = 1 + (r(i) - 3) * (1 - (x(i) - X1(i, 1)) / (X1(i, r(i)) - X1(i, 1))) * ((x(i) - X1(i, 1)) / (X1(i, r(i)) - X1(i, 1)));
q(i) = numerator / denominator;
end
abq_values = [a' b' q'];
L = []; L1 = []; X = []; Y = [];
p = N;
for k = 1:iter
fprintf("ITERATION=%d\n", k)
for i = 1:N-1
for j = 1:p-2
if(k == 1) % First iteration
% Input data is (x,y) or given data
L(i,j) = a(i) * X(i,j) + b(i);
L1(i,j) = alpha(i) * Y1(i,j) + (q(i) * X1(i,j));
else % More than one iteration
% Input data is (X1, Y1) OR output after the first iteration
L(i,j) = a(i) * X(i,j) + b(i);
L1(i,j) = alpha(i) * Y(i,j) + (q(i) * X1(i,j));
end
end
X = [X L(i,:)];
Y = [Y L1(i,:)];
end
X1 = X;
Y1 = Y;
X = [];
Y = [];
g = [X1' Y1'];
g = str2num(num2str(g,10));
g = unique(g, 'rows');
X1 = g(:, 1);
Y1 = g(:, 2);
p = length(X1);
end
plot(x, y, '.k', 'markersize', 80);
hold on;
plot(X1, Y1, 'b-');
Index exceeds the number of array elements. Index must not exceed 4. Please give me correct program
댓글 수: 0
RAJESH
2024년 9월 21일
term3 = r(i) * (y(i) - alpha(i) * Y1(i, 1)) + (x(i+1) - x(i)) * d(i) - alpha(i) * d1(i, 1) * (X1(i, t(i)) - X1(i, 1)) * ...
(1 - (x(i) - X1(i, 1)) / (X1(i, t(i)) - X1(i, 1))).^2 * (x(i) - X1(i, 1)) / (X1(i, t(i)) - X1(i, 1));
term4 = r(i) * (y(i+1) - alpha(i) * Y1(i, t(i))) - ((x(i+1) - x(i)) * d(i+1)) + alpha(i) * d1(i, t(i)) * (x1(i, t(i)) - x1(i, 1)) * ...
(1 - (x(i) - X1(i, 1)) / (X1(i, t(i)) - X1(i, 1))) * ((x(i) - X1(i, 1)) / (X1(i, t(i)) - X1(i, 1))).^2;
Index in position 1 exceeds array bounds. Index must not
exceed 1.
Error in rreplacetrrfcs (line 70)
term3 = r(i) * (y(i) - alpha(i) * Y1(i, 1)) + (x(i+1) - x(i)) * d(i) - alpha(i) * d1(i, 1) * (X1(i, t(i)) - X1(i, 1)) * ...
댓글 수: 1
DGM
2024년 9월 21일
편집: DGM
2024년 9월 21일
The error message means literally what it says. One of those indices is larger than the corresponding dimension of the array.
Which array is being indexed improperly? It's probably one of these 2-dimensional arrays:
- Y1(i, xxx)
- d1(i, xxx)
- X1(i, xxx)
Why is the array too short for the index? We have no way of knowing. It might be an initialization problem, or maybe a problem with incorrect array orientation or simply using the wrong variable name. We don't know what any of these abstract variable names mean or what is supposed to happen with them.
참고 항목
카테고리
Help Center 및 File Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!