Array Manipulation..

1 次查看(过去 30 天)
Mustafa
Mustafa 2011-7-12
Hi,
I have asked one question regarding a simple decrement of an array yesterday and got a good response. Today i am trying to manipulate that array further but i am kind of stuck in between loops and condtions. I am new to Matlab
It goes like this i am trying to decrement an array and once any particular element of that array arrives at zero, i am assigning that element as well as its immediate neighboring element with some value. What i need is that once that assigned value arrives at zero, it will increase the counter value by 1.
close all
clear all
clc
n = [11 12 13 24 3 7 14 11 12];
n_indx = zeros(1,12);
recv_count = 0;
send_count = 0;
send_count_1 = zeros(1,12);
recv_count_1 = zeros(1,12);
for i=1:40 % Just a loop so that the program can run for long (Not good programming practice)
while any(n)
n = max(0,n-1)
n_indx = find(n==0)
n(n_indx) = 3 ; % Assigning the value of "3" to the element that become zero
send_count_1(n_indx)=send_count+1 ; % Once an element become zero it should be recorded in this array at the same index
if (n(n_indx+1)>0) % Checking if the neighboring element is greater than zero
n(n_indx+1)=3 % Assigning the vlaue of "3" to the neighboring element
if (n(n_indx-1)>0) % Checking if the other neighboring element is also greater than zero
n(n_indx-1)=3 % Assigning the vlaue of "3" to the other neighboring element
% Now what i want is if all three elements (n_indx) && (n_indx+1)
% && (n_indx-1) are zero at the same point, they should be recorded
% into another counter
if (n(n_indx)==0) && (n(n_indx+1)==0) && (n(n_indx-1)==0)
recv_count_1(n_indx+1)=recv_count+1;
recv_count_1(n_indx-1)=recv_count+1;
% This gives me an error(That they are not scalar and i think they are scalar values).
break
end
end
end
end
end
  1 个评论
Mustafa
Mustafa 2011-7-12
Sorry for poor indentation and comments.
If the array goes out of dimensions or a problem of subscript occurr, dont worry about it. I will fix that

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by