how to manipulate this matrix?(need help pls)

1 visualizzazione (ultimi 30 giorni)
Derick Wong
Derick Wong il 20 Dic 2013
Modificato: Image Analyst il 20 Dic 2013
Hi,
I need help, and this is my last question.
Let say I have a column matrix X1 Y1 Z1 X2 Y2 Z2 . . . Xm Ym Zm
And on top of having m rows, I have n columns for it.
I need to use a method base on X1, X2 and X3 to get my X4, X5 till Xm.
The method in order to find X4 is X4=(X1+X2+X3)/3. Once X4 is calculated, we use the X4 to calculate X5 which now the it will turn out to be X5=(X2+X3+X4)/3 and find X6 using the found X5 and X4, X6=(X3+X4+X5)/3 and so on until we find Xm.This goes same for Y and Z.
My question is, how do we come out with such matrix given that it has X1 at row 1, X2 at row 4 and X3 at row 7 so on.X4 must be at row 10. This goes same for Y and Z for m row and n columns.

Risposte (2)

Azzi Abdelmalek
Azzi Abdelmalek il 20 Dic 2013
Modificato: Azzi Abdelmalek il 20 Dic 2013
A=[1;11;111;2;22;222;3;33;333;4;44;444;5;55;555;6;66;666]
x=A(1:3:end)
for k=4:numel(x)
out(k)=mean(x(k-3:k-1))
end
Use the same for y
y=A(2:3:end)
and for z
z=A(3:3:end)

Image Analyst
Image Analyst il 20 Dic 2013
Modificato: Image Analyst il 20 Dic 2013
Because your x changes as you go along, you can't simply use nromal linear filtering, like the mean() or conv() or something like that. You're going to have to use recursion (which is complicated) or use a for loop where you calculate the mean over the prior 3 values and then replace x at that element with the newly calculated mean so that you can use the new x in your next calculation. Here's a full demo (over x only):
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
numberOfCoords = 15;
xyz = randi(9, numberOfCoords*3, 1) % Create sample data
% Extract out x, y, and x:
x = xyz(1:3:end);
y = xyz(2:3:end);
z = xyz(3:3:end);
% Plot the original x.
plot(x, 'rd', 'MarkerSize', 9, 'LineWidth', 2);
hold on;
% Process x
xMean = zeros(size(x)); % Preallocate space for speed.
% Assign first two means
xMean(1) = x(1);
xMean(2) = x(1);
xMean(3) = (x(1)+x(2))/2;
for row = 4 : length(x)
thisMean = sum(x(row-3:row-1))/3;
% Replace x
x(row) = thisMean
% Store in result/output array
xMean(row) = thisMean
end
% Plot the new x.
plot(x, 'ro', 'MarkerSize', 12, 'LineWidth', 2);
plot(xMean, 'b*', 'MarkerSize', 12, 'LineWidth', 2);
grid on;
ylabel('x or xMean', 'FontSize', 15);
xlabel('Element number', 'FontSize', 15);
legend('Original x', 'New x', 'xMean', 'Location', 'SouthEast');
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Repeat for y and z.
What you'll notice is that both x and the output, which I called xMean, both approach the same value. This is because you're replacing x as you go and using this new x in future calculations, so obviously both will approach the mean very quickly.

Categorie

Scopri di più su MATLAB Mobile Fundamentals in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by