divide and conquer

1 visualizzazione (ultimi 30 giorni)
Majid Al-Sirafi
Majid Al-Sirafi il 23 Mar 2012
Modificato: DGM il 21 Set 2024
please
anyone can give me source code about divide and conquer method
thank you
  2 Commenti
Oleg Komarov
Oleg Komarov il 25 Mar 2012
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
Daniel Shub
Daniel Shub il 25 Mar 2012
My guess is this is a sorting question...

Accedi per commentare.

Risposte (3)

Walter Roberson
Walter Roberson il 25 Mar 2012
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 Commenti
Walter Roberson
Walter Roberson il 22 Mag 2017
Peter Weigel comments to Walter Roberson:
This comment is detrimental to the health of the MathWorks forums.
Walter Roberson
Walter Roberson il 22 Mag 2017
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.

Accedi per commentare.


RAJESH
RAJESH il 29 Ago 2024
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

RAJESH
RAJESH il 21 Set 2024
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 Commento
DGM
DGM il 21 Set 2024
Modificato: DGM il 21 Set 2024
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.

Accedi per commentare.

Community Treasure Hunt

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

Start Hunting!

Translated by