Array Manipulation..

7 次查看(过去 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

请先登录,再进行评论。

回答(1 个)

Vidhi Agarwal
Vidhi Agarwal 2024-7-31
Hi Mustafa,
I assume you are encountering some issues with your code. Following is the attached code giving required output.
n = [11 12 13 24 3 7 14 11 12];
processed = false(size(n)); % Array to keep track of processed elements
recv_count = 0;
send_count = 0;
send_count_1 = zeros(size(n));
recv_count_1 = zeros(size(n));
for i = 1:40 % Loop to run the program for a long time
while any(n > 0)
n = max(0, n-1);
n_indx = find(n == 0 & ~processed); % Find indices where n is zero and not processed
% Iterate over each index where the element is zero
for idx = n_indx
if n(idx) == 0 && ~processed(idx) % Ensure the element is still zero and not processed
n(idx) = 3; % Assigning the value of "3" to the element that becomes zero
send_count = send_count + 1; % Increment the send counter
send_count_1(idx) = send_count; % Record the zero element in this array
processed(idx) = true; % Mark as processed
% Check and assign value to the neighboring elements
if idx > 1 && n(idx-1) > 0
n(idx-1) = 3;
end
if idx < length(n) && n(idx+1) > 0
n(idx+1) = 3;
end
end
end
% Now check if all three elements (n_indx), (n_indx+1), and (n_indx-1) are zero
for idx = n_indx
if idx > 1 && idx < length(n) && n(idx) == 0 && n(idx+1) == 0 && n(idx-1) == 0
recv_count = recv_count + 1; % Increment the receive counter
recv_count_1(idx) = recv_count;
recv_count_1(idx+1) = recv_count;
recv_count_1(idx-1) = recv_count;
end
end
end
end
disp('send_count_1:');
disp(send_count_1);
disp('recv_count_1:');
disp(recv_count_1);
Hope that helps!

类别

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