How to iterate through a column vector elements one by one?

Hello Guys! I am new to matlab and working on a project. I want to iterarte through a column vector and retrive element of column one by one into another variable. That is, I want to retrive first element from a matrix of 1382x1 and then compute it and then the next element and so on. Kindly Help me out. I knw it would be using for loop but I cant find a solution.

4 Comments

I want to retrive first element from a matrix of 1382x1 and then compute it and then the next element and so on.
?
I am making a project on fall detection using mobile acceleration sensors whose algorithm is to be made on matlab platform. Accelerometers in mobile devices have three parameters, accel_x, accel_y,accel_z. So to compute the final acceleration we do: Acc = sqrt(accel_x^2+accel_y^2+accel_z^2). So I when I record the acceleration from mobile phone sensors I got a column matrix of 1382x1 for each parameter x,y&z. So is there any way to calculate the final acceleration (Acc) from those columns? and the final result could be a single value instead of 1382 values? Hope uunderstand.
Acc = sqrt(accel_x.^2+accel_y.^2+accel_z.^2)
or
accel = [accel_x accel_y accel_z];
Acc = sqrt(sum(accel.^2,2))
or
Acc = sqrt(sum([accel_x accel_y accel_z].^2,2))
So how do you want to get a single value from those 1382 Acc values?
>> m=mobiledev
%m =
%Current Sensor Values:
%Acceleration: [0x3 double] (Waiting for data)
%AngularVelocity: [0x3 double] (Waiting for data)
[a,t]= accellog(m); %retriving aceeleration data fom mobile
[b,t]= angullog(m): %retriving angular velocity data fom mobile
LFT_accel=0.3; % Lower Fall Threshold 0.3g (0.3*9.8m^2/s)
UFT_accel=2.8; % Upper Fall Threshold 2.8g
UFT_ang=200; % Upper Fall Threshold 200 degree/s
% receive/and judge six data are processed in one loop
memLen=10; % The length of motion data memory
motionData=zeros(6,memLen); % Motion data memery
memPoint=1; % Current processing time point
lastPoint=0; % The last time point before current one
while(memPoint~=0) % with a period of fall duration
accel_x = a(1,1);
accel_y = a(1,2);
accel_z = a(1,3);
ang_x = b(1,1);
ang_y = b(1,2);
ang_z = b(1,3);
motionData(1,memPoint)=accel_x; % Acceleration on axis x
motionData(2,memPoint)=accel_y; % Acceleration on axis y
motionData(3,memPoint)=accel_z; % Acceleration on axis z
motionData(4,memPoint)=ang_x; % Angular rate on axis x
motionData(5,memPoint)=ang_y; % Angular rate on axis y
motionData(6,memPoint)=ang_z; % Angular rate on axis z
Acc=sqrt(accel_x^2+accel_y^2+accel_z^2);
if (Acc<=LFT_accel)
lastPoint=mod(memPoint-9,memLen);
Acc=sqrt(motionData(1,lastPoint)^2+motionData(2,lastPoint)^2+motionData(3,lastPoint)^2);
Angular=sqrt(motionData(4,lastPoint)^2+motionData(5,lastPoint)^2+motionData(6,lastPoint)^2);
if (Acc>=UFT_accel&& Angular>=UFT_ang)
disp('A Fall Has Happened!');
end
else
disp('No Fall');
end
memPoint=mod(memPoint+1,memLen);
end
So in this code I can calculate the Acc and Angular for first element of each column.. I want to calculate it for each next values....
accel_x = a(1,1);
accel_y = a(1,2);
accel_z = a(1,3);
How can I do that? Please help!

Sign in to comment.

Answers (1)

v = rand(1382,1); % 1382x1 column vector
N = numel(v);
final_result = zeros(N,1); % "another variable", the same size as v
for ii = 1:N
% retrieve ii-th element of v:
current_value = v(ii);
% do something with current_value to get a result:
current_result = current_value^2;
% store the result in the final_result vector:
final_result(ii) = current_result;
end
Or, without the use of the temporary variables current_value and current_result:
for ii = 1:N
% compute the result for the ii-th element of v, and
% store it in the final_result vector:
final_result(ii) = v(ii)^2;
end
However, depending on what your computation entails, you may be able to avoid the loop entirely. For example, the above can be written as:
final_result = v.^2;

Categories

Find more on MATLAB in Help Center and File Exchange

Products

Release

R2022b

Asked:

on 5 Dec 2022

Commented:

on 7 Dec 2022

Community Treasure Hunt

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

Start Hunting!