copying matrix values based on another matrix value in MATLAB - arrays

Hi I am trying to perform this on MATLAB
A =
64 2 3 61 60 6 7 57
9 55 54 12 13 51 50 16
17 47 46 20 21 43 42 24
40 26 27 37 36 30 31 33
32 34 35 29 28 38 39 25
41 23 22 44 45 19 18 48
49 15 14 52 53 11 10 56
8 58 59 5 4 62 63 1
I want to select values from A based on F
F =
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
-1 -1 -1 -1 -1 -1 -1 -1
0 0 0 0 0 0 0 0
-1 -1 -1 -1 -1 -1 -1 -1
-1 -1 -1 -1 -1 -1 -1 -1
-1 -1 -1 -1 -1 -1 -1 -1
I want this output
u =
40 26 27 37 36 30 31 33
41 23 22 44 45 19 18 48
49 15 14 52 53 11 10 56
8 58 59 5 4 62 63 1
This means that I want all the values corresponding to '-1' in a matrix. i tried u=A(F==-1) ... but it gives me a single column with all the values like this:
u =
40
41
49
8
26
23
15
58
27
22
14
59
37
44
52
5
36
45
53
4
30
19
11
62
31
18
10
63
33
48
56
1

This will do it:-
u=-A.*F;
u(all(u==0,2),:)=[] %Removing rows containing zeros
Another solution:-
u=reshape(A(F==-1),4,8)

Related

how the broker in cloudsim works

I am working on chicken swarm optimization when the algorithm return the best solution the broker seem to have a different ordering
best solution
Cloudlet Vm
-------- ---------
99 1
52 2
62 7
63 13
64 3
68 14
71 6
94 0
97 8
13 9
53 10
91 11
16 12
1 4
61 5
15 15
33 16
34 17
35 18
43 19
44 1
45 2
46 7
47 13
49 3
42 14
48 6
50 0
51 8
37 9
40 10
36 11
39 12
41 4
38 5
6 15
31 16
19 17
65 18
88 19
89 1
66 2
9 7
92 13
93 3
18 14
98 6
14 0
30 8
12 9
2 10
96 11
11 12
29 4
77 5
87 15
70 16
76 17
78 18
81 19
82 1
69 2
56 7
57 13
32 3
85 14
75 6
80 0
83 8
60 9
86 10
79 11
74 12
84 4
67 5
28 15
90 16
27 17
21 18
72 19
95 1
73 2
26 7
23 13
25 3
24 14
3 6
20 0
59 8
58 9
0 10
55 11
54 12
4 4
22 5
17 15
8 16
10 17
7 18
5 19
printing the result after receiving from broker
Cloudlet ID STATUS Data center ID VM ID
99 SUCCESS 2 1
52 SUCCESS 2 2
62 SUCCESS 2 7
64 SUCCESS 2 3
71 SUCCESS 2 6
97 SUCCESS 2 8
63 SUCCESS 2 13
68 SUCCESS 2 14
94 SUCCESS 2 0
53 SUCCESS 2 10
13 SUCCESS 2 9
91 SUCCESS 2 11
16 SUCCESS 2 12
1 SUCCESS 2 4
61 SUCCESS 2 5
15 SUCCESS 2 15
35 SUCCESS 2 18
44 SUCCESS 2 1
33 SUCCESS 2 16
34 SUCCESS 2 17
45 SUCCESS 2 2
46 SUCCESS 2 7
49 SUCCESS 2 3
48 SUCCESS 2 6
51 SUCCESS 2 8
47 SUCCESS 2 13
42 SUCCESS 2 14
50 SUCCESS 2 0
43 SUCCESS 2 19
40 SUCCESS 2 10
37 SUCCESS 2 9
36 SUCCESS 2 11
39 SUCCESS 2 12
41 SUCCESS 2 4
38 SUCCESS 2 5
6 SUCCESS 2 15
31 SUCCESS 2 16
19 SUCCESS 2 17
65 SUCCESS 2 18
89 SUCCESS 2 1
88 SUCCESS 2 19
66 SUCCESS 2 2
9 SUCCESS 2 7
93 SUCCESS 2 3
92 SUCCESS 2 13
18 SUCCESS 2 14
98 SUCCESS 2 6
14 SUCCESS 2 0
30 SUCCESS 2 8
2 SUCCESS 2 10
12 SUCCESS 2 9
96 SUCCESS 2 11
any ideas why this this happened ?

Custom type with pointer in COMMON block not being passed properly

(Edited 2021-09-06 13:30 CEST after I found out that the problem is caused at a different place of my code already)
I am working on some code (not my own) where they use a custom type for dynamic integer arrays. One instance of this type is passed via a common block between routines. It looks like this is not working properly or as expected, since the arrays take different values after being passed to a new routine. A minimal example looks like this:
implicit none
type intdynarr2
sequence
integer, pointer::i(:,:)
end type intdynarr2
type(intdynarr2):: my_array
integer j,k
common/carrays/my_array
allocate(my_array%i(1:10,1:10))
do j=1,10
do k=1,10
my_array%i(j,k)=j*k
enddo
enddo
write(*,*) my_array%i
call printarr
end
subroutine printarr
implicit none
type intdynarr2
sequence
integer, pointer::i(:,:)
end type intdynarr2
type(intdynarr2):: my_array
common/carrays/my_array
write(*,*) my_array%i
end subroutine
The output of this after compiling with gfortran in version 8.2.0 looks like this:
1 2 3 4 5 6 7 8 9 10 2 4 6 8 10 12 14 16 18 20 3 6 9 12 15 18 21 24 27 30 4 8 12 16 20 24 28 32 36 40 5 10 15 20 25 30 35 40 45 50 6 12 18 24 30 36 42 48 54 60 7 14 21 28 35 42 49 56 63 70 8 16 24 32 40 48 56 64 72 80 9 18 27 36 45 54 63 72 81 90 10 20 30 40 50 60 70 80 90 100
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
while I expected it to be:
1 2 3 4 5 6 7 8 9 10 2 4 6 8 10 12 14 16 18 20 3 6 9 12 15 18 21 24 27 30 4 8 12 16 20 24 28 32 36 40 5 10 15 20 25 30 35 40 45 50 6 12 18 24 30 36 42 48 54 60 7 14 21 28 35 42 49 56 63 70 8 16 24 32 40 48 56 64 72 80 9 18 27 36 45 54 63 72 81 90 10 20 30 40 50 60 70 80 90 100
1 2 3 4 5 6 7 8 9 10 2 4 6 8 10 12 14 16 18 20 3 6 9 12 15 18 21 24 27 30 4 8 12 16 20 24 28 32 36 40 5 10 15 20 25 30 35 40 45 50 6 12 18 24 30 36 42 48 54 60 7 14 21 28 35 42 49 56 63 70 8 16 24 32 40 48 56 64 72 80 9 18 27 36 45 54 63 72 81 90 10 20 30 40 50 60 70 80 90 100
Can anyone tell me what is going wrong here and how to fix this?

C Arrays Not Retaining Values

I'm writing a simple Base64 decoding function in C. The output of this function looks correct (based on comparing the result with existing tools) when using the printf() debugging lines as below:
unsigned char * b64decode(char *line)
{
char *idx;
unsigned char *decode;
int i,j,len,dlen,hold = 0;
idx = strchr(line, '\n'); //index of newline
len = idx - line; //lenght of string
dlen = 3 * (len / 4); //decoded length
decode = malloc((sizeof(unsigned char) * dlen) + 1);
pad = 0;
for (i = 0; i <= len; i++) { //deindex from ASCII
line[i] = deindex(line[i]);
}
for(i = 0, j = u0; i < len; i++,j++) {
hold |= (line[i] << 18);
hold |= (line[++i] << 12);
hold |= (line[++i] << 6);
hold |= line[++i];
decode[j] = hold >> 16;
printf("%d ", decode[j]);
decode[++j] = (hold >> 8) & 0xFF;
printf("%d ", decode[j]);
decode[++j] = hold & 0xFF;
printf("%d ", decode[j]);
hold = 0;
}
if (pad) //terminate before padding
decode[dlen - pad] = '\0';
return decode;
}
Which produces the (partial) output:
29 66 31 77 11 15 2 31 79 19 78 60 26 105 101 31 73 28 14 78 19 1 11 7
78 27 1 22 69 54 0 30 1 73 100 32 84 29 29 67 51 83 78 101 82 6 0 71 84
28 13 69 77 7 4 12 83 18 60 12 30 8 73 26 9 17 79 20 76 33 26 71 43 0 5
29 71 89 17 4 9 0 100 38 7 83 0 55 22 6 12 26 23 65 29 1 82 84 48 95 0 32
19 10 5 71 79 18 72 8 69 78 101
The problem arises when I try to access this array after exiting the for loop with those printf() lines. This for loop (maximum i value is arbitrary) ...
for (i = 0; i < 100; i++) {
printf("%d ", decode[i]);
}
Produces this output...
29 66 31 77 11 15 2 31 79 19 78 60 26 105 101 31 73 28 14 78 19 1 11 7 78
27 1 22 69 54 0 30 1 73 100 32 84 29 29 67 51 83 78 101 82 6 0 71 84 28
13 69 77 7 4 12 83 18 60 12 30 8 73 26 9 17 79 20 76 33 26 71 43 0 5 29
71 89 17 4 9 0 100 38 7 83 0 55 22 6 12 26 23 65 29 1 82 84 48 95 0 32 19
10 5 71 79 168 187 158 23 131 127 0 0 168 187 158 23 131 127 0 0 0 0 0 0
0 0 0 0 65 0 0 0 0 0 0 0 88 187 158 23 131 127 0 0 88 187 158 23 131 127
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 64 0
0 0 0 0 0 0 16 16 0 0 0 0 0 0 72 85 73 102
Can anyone let me know what I'm doing wrong?
OK folks, found the issue. The function as shown here works, the issue turned out to be with a realloc() called on *decode in error in the function which handles my function above. Fixing this produced the correct results.
Guess this is a swift lesson in providing more context!

Sorting array of numbers in C

// code to sort array of 16 numbers, but output isnt quite correct.
// must use pointers to array addresses
// final output is -451 993 384 201 89 77 38 28 16 12 7 1 0 -2 -5 -17
// as you can see -451 isn't in the right place.
output
-451 7 993 1 0 16 -5 12 89 28 77 384 -2 38 -17 201
-451 -17 993 7 1 16 0 12 89 28 77 384 -2 38 -5 201
-451 993 -17 7 1 16 0 12 89 28 77 384 -2 38 -5 201
-451 993 7 -17 1 16 0 12 89 28 77 384 -2 38 -5 201
-451 993 7 1 -17 16 0 12 89 28 77 384 -2 38 -5 201
-451 993 16 7 1 -17 0 12 89 28 77 384 -2 38 -5 201
-451 993 16 7 1 0 -17 12 89 28 77 384 -2 38 -5 201
-451 993 16 12 7 1 0 -17 89 28 77 384 -2 38 -5 201
-451 993 89 16 12 7 1 0 -17 28 77 384 -2 38 -5 201
-451 993 89 28 16 12 7 1 0 -17 77 384 -2 38 -5 201
-451 993 89 77 28 16 12 7 1 0 -17 384 -2 38 -5 201
-451 993 384 89 77 28 16 12 7 1 0 -17 -2 38 -5 201
-451 993 384 89 77 28 16 12 7 1 0 -2 -17 38 -5 201
-451 993 384 89 77 38 28 16 12 7 1 0 -2 -17 -5 201
-451 993 384 89 77 38 28 16 12 7 1 0 -2 -5 -17 201
-451 993 384 201 89 77 38 28 16 12 7 1 0 -2 -5 -17
total exchanges: 68
#include <stdio.h>
#define N 16
int xchg();
int main() {
int numbers[16] = {7, 1, 993, -5, 0, 16, -451, 12, 89, 28, 77, 384, -2, 38, -17, 201};
int cntr, cntr2, cntr3;
int chgNum;
for(cntr = 0; cntr < N; cntr++){
for(cntr2 = 1; cntr2 < N; cntr2++){
chgNum += xchg(&numbers[cntr], &numbers[cntr2]);
}
for(cntr3 = 0; cntr3 < N; cntr3++){
if(cntr3 == 15){
printf("%d", numbers[cntr3]);
}
else {
printf("%d ", numbers[cntr3]);
}
}
printf("\n");
}
printf("total exchanges: %d\n", chgNum);
return 0;
}
int xchg(int *p1, int *p2) {
int tmp = 0;
if(*p2 < *p1){
tmp = *p1;
*p1 = *p2;
*p2 = tmp;
return 1;
}
else {
return 0;
}
}
You need to change your loop in main.
for(cntr2 = cntr+1; cntr2 < N; cntr2++){
You might also want to check if xchg is giving you the direction ascending/descending sort order or if you need to invert your exchange condition.
Also: you forgot to initialize chgNum to zero.
Ok the problem is that the inner for loop starts at 1, after the first iteration it will be wrong it needs to start at the position that the first loop is at, i.e. cntr2 = cntr.
A few other things:
The forward declaration of the `xchng` function isn't correct.
The if else print statement doesn't do anything it will print the same thing no matter what.
If you are incrementing a number using `++` operator and it isn't being assigned to anything, always use `++(int)`, it requires fewer operations.
Finally, there are some nasty variable names, it doesn't hurt to be more descriptive. Also you define a macro which is the size of the array, but you don't define the array using it, int numbers[N] would make more sense as then at least you can see why the the macro is used in the loop.

Why would this code count different number of lines from linux and windows text files?

This code,
lineCount = 1;
do{ //find the line count in the file
c = fgetc(fp);
if(c == '\n') lineCount++;
}
while(c != EOF);
fclose(fp);
counts 6 lines from this file
5 7 9 3 2 10 1 11 6 4 14 0 12 8 13
3 4 10 8 0 12 13 2 7 1 9 5 6 14 11
12 14 11 8 0 7 3 5 1 6 4 13 10 9 2
14 11 13 0 2 12 9 3 5 7 1 6 8 4 10
0 1 8 6 5 3 11 2 7 9 4 12 10 14 13
and counts 5 lines from this file
39 47 37 30 7 38 17 49 11 1 29 41 25 19 10 45 23 0 32 15 2 9 4 6 21 40 20 24 5 31 34 3 33 48 44 27 14 26 28 35 16 42 46 36 12 8 22 13 18 43
37 13 24 28 34 27 5 41 36 29 44 26 0 15 40 31 23 35 9 8 4 33 21 6 11 49 2 7 43 32 16 1 30 42 39 14 45 10 38 22 19 17 20 25 18 47 48 46 3 12
0 49 26 20 14 12 10 3 9 23 15 37 5 32 4 42 25 46 38 45 40 19 22 1 39 29 7 41 33 13 30 35 11 6 18 31 21 28 24 36 16 43 27 34 44 17 2 8 47 48
0 1 20 11 28 3 43 9 15 25 45 29 33 19 48 18 17 16 14 34 10 7 42 4 37 41 22 30 23 21 32 39 2 46 8 36 40 27 31 13 6 38 12 5 44 26 35 24 49 47
15 30 18 7 34 25 43 14 38 48 40 9 33 26 28 27 21 0 20 10 47 8 11 32 12 5 36 4 46 42 6 29 13 31 23 17 39 35 19 49 24 41 44 16 37 45 2 1 22 3
What would the reason be?
EDIT
I created a file with 5 lines in Windows and 5 lines in Linux, Windows file counts 5 and Linux file counts 6. Why?
You should verify that the two files actually only have four newline characters. In Linux, use the xxd command to dump the contents of your files in hexadecimal and then count the number of a characters (because newline characters are the letter 'a' in hex):
$ cat foo.txt
5 7 9 3 2 10 1 11 6 4 14 0 12 8 13
3 4 10 8 0 12 13 2 7 1 9 5 6 14 11
12 14 11 8 0 7 3 5 1 6 4 13 10 9 2
14 11 13 0 2 12 9 3 5 7 1 6 8 4 10
0 1 8 6 5 3 11 2 7 9 4 12 10 14 13
$ xxd -p foo.txt
352037203920332032203130203120313120362034203134203020313220
38203133200a332034203130203820302031322031332032203720312039
20352036203134203131200a313220313420313120382030203720332035
20312036203420313320313020392032200a313420313120313320302032
20313220392033203520372031203620382034203130200a302031203820
362035203320313120322037203920342031322031302031342031330a <- note the 'a' at the end
$ xxd -p foo.txt | grep -o a | wc -l
5
You'll likely find that there are indeed five newline characters in your "6 line" file.
You start the count at 1 and increment for each newline encountered. If a file has one line—one string of non-newline characters followed by one newline—and then ends, you start the count at 1 and then increment it once, giving a line count of 2.
Perhaps want you want to do is start the count at 0, increment for each newline, and take note of any final non-newline-terminated-line? (That's a little bit harder.)
If test data were to be assumed to be the same as posted above and fp were to be opened as a text file:
I tested this on Linux and Windows. Both print 6(incorrect of-course because of the logic, which assumes that there is atleast 1 line)
gcc - 4.5.3
microsoft compiler cl for VS2010.
Also, linecount must start from 0 instead of 1.

Resources