Help to complete my code!!!

Status
Not open for further replies.

bincy thankachan

Newbie level 6
Joined
Oct 10, 2011
Messages
11
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,283
Activity points
1,377
I'm working with PSO algorithm. This is the code, i want the output to be added and give a value.
a=[2 3 7 1 4 2 1 2 8 2 5 9 2 10 12 1 2 6];
b=unique(a);
[b m n]=unique(a);
counts=accumarray(n),1)
for k=1:numel(counts)
fprintf('the value %d appears %d times in a\n',...
b(k),counts(k));
end
for i=1:counts
if(counts(i)==1)
disp('q=0');
end
if(counts(i)==2)
disp('r=0');
end
if(counts(i)==3)
disp('s=3');
end
if(counts(i)==4)
disp('t=6');
end
if(counts(i)==5)
disp('u=10');
end
if(counts(i)==6)
disp('v=15');
end
end

The output is coming like this:
counts =

3
6
1
1
1
1
1
1
1
1
1

the value 1 appears 3 times in a
the value 2 appears 6 times in a
the value 3 appears 1 times in a
the value 4 appears 1 times in a
the value 5 appears 1 times in a
the value 6 appears 1 times in a
the value 7 appears 1 times in a
the value 8 appears 1 times in a
the value 9 appears 1 times in a
the value 10 appears 1 times in a
the value 12 appears 1 times in a
s=3
v=15
q=0

These values should be added and give the output as 18. I'm new to matlab so pls help me out.
Thanks.
 

I think this is what you need:
Code:
a=[2 3 7 1 4 2 1 2 8 2 5 9 2 10 12 1 2 6];
b=unique(a);
[b m n]=unique(a);
counts=accumarray(n(:),1)
for k=1:numel(counts)
    fprintf('the value %d appears %d times in a\n',...
    b(k),counts(k));
end
 q = 0; r = 0; s = 0; t = 0; u = 0; v = 0;
for i=1:counts
    if(counts(i)==1)
        q = 0;
        disp('q=0');
    end
    if(counts(i)==2)
        r = 0;
        disp('r=0');
    end
    if(counts(i)==3)
        s = 3;
        disp('s=3');
    end
    if(counts(i)==4)
        t = 6;
        disp('t=6');
    end
    if(counts(i)==5)
        u = 10;
        disp('u=10');
    end
    if(counts(i)==6)
        v = 15;
        disp('v=15');
    end
end
sum = q + r + s + t + u + v;
fprintf('Sum = %d\n',sum);

Regards,
Salman
 

Help me out of this code please!

This is my program
a=[2 3 7 1 4 2 1 16 8 2 5 9 15 10 12 1 26 6];
b=unique(a);
[b m n]=unique(a);
counts=accumarray(n),1)
for k=1:numel(counts)
fprintf('the value %d appears %d times in a\n',...
b(k),counts(k));
end
q = 0; r = 0; s = 0; t = 0; u = 0; v = 0;
for i=1:counts
if(counts(i)==1)
q = 0;
disp('q=0');
end
if(counts(i)==2)
r = 0;
disp('r=0');
end
if(counts(i)==3)
s = 3;
disp('s=3');
end
if(counts(i)==4)
t = 6;
disp('t=6');
end
if(counts(i)==5)
u = 10;
disp('u=10');
end
if(counts(i)==6)
v = 15;
disp('v=15');
end
end
sum = q + r + s + t + u + v;
fprintf('Interference number = %d\n',sum);

Output is cuming like dis:

counts =

3
3
1
1
1
1
1
1
1
1
1
1
1
1

the value 1 appears 3 times in a
the value 2 appears 3 times in a
the value 3 appears 1 times in a
the value 4 appears 1 times in a
the value 5 appears 1 times in a
the value 6 appears 1 times in a
the value 7 appears 1 times in a
the value 8 appears 1 times in a
the value 9 appears 1 times in a
the value 10 appears 1 times in a
the value 12 appears 1 times in a
the value 15 appears 1 times in a
the value 16 appears 1 times in a
the value 26 appears 1 times in a
s=3
s=3
q=0
Interference number = 3

But the output must cum as 6(adding all the outputs). Can anyone help me out.
Thanks.

Don't start new threads with the same content. Threads merged [alexan_e]
 

Re: Help me out of this code please!

You should have stated this problem in the same thread. No need to start a new thread for problem relating to same code.
Here's what you need to change:
Code:
a=[2 3 7 1 4 2 1 16 8 2 5 9 15 10 12 1 26 6];
b=unique(a);
[b m n]=unique(a);
counts=accumarray(n(:),1)
for k=1:numel(counts)
    fprintf('the value %d appears %d times in a\n',...
    b(k),counts(k));
end
 q = 0; r = 0; s = 0; t = 0; u = 0; v = 0;
for i=1:counts
    if(counts(i)==1)
        q = 0;
        disp('q=0');
    end
    if(counts(i)==2)
        r = 0;
        disp('r=0');
    end
    if(counts(i)==3)
        s = s + 3;
        disp('s=3');
    end
    if(counts(i)==4)
        t = t + 6;
        disp('t=6');
    end
    if(counts(i)==5)
        u = u + 10;
        disp('u=10');
    end
    if(counts(i)==6)
        v = v + 15;
        disp('v=15');
    end
end
sum = q + r + s + t + u + v;
fprintf('Sum = %d\n',sum);

And one more suggestion is that you should check out some basic matlab tutorials online. Just google it.

Regards,
Salman
 
Reactions: taroot

    taroot

    Points: 2
    Helpful Answer Positive Rating
how can i give matrix as input arguments? that is each time i need to give a different matrix from command window. how can i do that?
Also i need to make this whole program come under one function.
pls help me.
Thanks.
 
Reactions: taroot

    taroot

    Points: 2
    Helpful Answer Positive Rating
Use "input" function of MATLAB.
like:
Code:
x = input('Enter Matrix: ');
OutPut:
Enter Matrix: (here it will wait for input like) [2 3 1 4 5 8 9 10](enter)

For further details on how to use "input" function, check this link: Request user input - MATLAB
For details on how to make functions in MATLAB: Declare function - MATLAB
Regards,
Salman
 
Reactions: taroot

    taroot

    Points: 2
    Helpful Answer Positive Rating
what is happening here...for the above code i'm giving this matrix:

a=[9 8 9 2 3 12 4 5 9 12 8 2 12 5 9 11 11 4]
b=unique(a);
[b m n]=unique(a);
counts=accumarray(n),1)
for k=1:numel(counts)
fprintf('the value %d appears %d times in a\n',...
b(k),counts(k));
end
q = 0; r = 0; s = 0; t = 0; u = 0; v = 0;
for i=1:counts
if(counts(i)==1)
q = q + 0;
disp('q=0');
end
if(counts(i)==2)
r = r + 0;
disp('r=0');
end
if(counts(i)==3)
s = s + 3;
disp('s=3');
end
if(counts(i)==4)
t = t + 6;
disp('t=6');
end
if(counts(i)==5)
u = u + 10;
disp('u=10');
end
if(counts(i)==6)
v = v + 15;
disp('v=15');
end
end
sum = q + r + s + t + u + v;
fprintf('Sum = %d\n',sum);

output is coming as:

counts =

2
1
2
2
2
4
2
3

the value 2 appears 2 times in a
the value 3 appears 1 times in a
the value 4 appears 2 times in a
the value 5 appears 2 times in a
the value 8 appears 2 times in a
the value 9 appears 4 times in a
the value 11 appears 2 times in a
the value 12 appears 3 times in a
r=0
q=0
Sum = 0

Here 9 is appearing 4 times,value must be displayed as t=6. Also the sum must given some value rather zero.
why is this happening? help me out.
Thanks.
 

Well your loop "for i=0:counts" runs for two time only since counts = counts(1) = 2.
Try this instead "for i=0:size(counts)".
 
Reactions: taroot

    taroot

    Points: 2
    Helpful Answer Positive Rating
In a mesh network can all the nodes having multiple radios be assigned the same channel number? Is there any constraint regarding that?
What about the interference for such a network?
pls let me know.
Thanks.
 

Given a matrix:[3 4 4 2 8 9 5 12 11 11 9 7]
Then how can we replace the repeated value with another one? For ex. 4 is repeated two times...so can this matrix be replaced as non-repeated values like
[3 4 5 2 8 9 5 12 11 1 9 7]?
How to modify it? Please help me out.
Thanks.
 

Then how can we replace the repeated value with another one?
you want to replace repeated value with any value other then that or any value other then the whole matrix.?
Just for confirmation.
 
Reactions: taroot

    taroot

    Points: 2
    Helpful Answer Positive Rating
I want to replace repeated value with any other value in between 1 to 12.

Also is it possible to do like this:
A matrix given as:
[1 4 5
5 5 5
6 7 12]
Here a row should not contain all same value like here '5'. It can repeat twice in a row but should not thrice. It must take any value between 1 to 12. Is it possible to modify?
Thanks.
 

here's the code:
Code:
clc
clear
m = [1 1 1; 5 5 5; 12 12 12];
n = m;
s = size(m);
k = 0; vac = 0;
for i = 1 : s(1)
    for j = 1 : s(2)-1
        for z = j+1 : s(2) 
            if(n(i,j) == n(i,z))
                vac = vac+1;
                if(vac >= 2)
                    if(n(i,z) < 12)
                         n(i,z) =  n(i,z) + 1;
                    else n(i,z) =  n(i,z) - 1;
                    end
                end
            end            
        end
        vac = 0;
    end
end
m
n

regards,
Salman
 
Reactions: taroot

    taroot

    Points: 2
    Helpful Answer Positive Rating
Given a matrix:
A=[1 2 3 4 5 6 7 8 9]
here i want this matrix to be written in the form : [ 1 2 3
4 5 6
7 8 9].
By using reshape function as: reshape(A,3,3) its giving output as [1 4 7
2 5 8
3 6 9]
After that transpose function can be used but can i get the output which i want without using it.
Also after bringing some changes i should put the matrix back in the form i.e a single row.
The input matrix is not like:[1 2 3;4 5 6;7 8 9].It is a single row.
Is it possible to do? Please help me out.
Thanks.
 

You can do it like this:

Code:
A=[1 2 3 4 5 6 7 8 9];
Mat(1,1:3) = A(1:3);
Mat(2,1:3) = A(4:6);
Mat(3,1:3) = A(7:9);
Mat 

Back = [Mat(1,1:3) Mat(2,1:3) Mat(3,1:3)];
Back


Regards,
Salman
 

The above code works well. The 'Back' gives the matrix in a single row but the problem is when I'm giving a matrix with ex.6 rows I'm not able to get the whole matrix back in the previous format.

For Ex.
A=[1 2 3 4 5 6 7 8 9
3 5 6 8 2 1 10 2 3
6 7 6 8 9 10 12 11 5]
Here I made it 3by3 matrix and perform some function and obtained result with the help of above provided code. But how to get back the matrix as A when using multiple rows?
Please help me out.
Thanks.
 

A=randint(10,18,[1 12])
for m=1:10
s=1;
for i=1:6
for k=1:3
M(i,k)=A(m,s);
s=s+1;
end
end
M
end

Here in the output all the matrix is printed with the same name 'M'.
But i need to give different names to all the matrix.
Is it possible? Please help me out.
Thanks.
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…