I have created an array in Matlab and am facing an issue that when I ask to show all value of array not using colon, e.g., allfriendsmarks=m(), the result is:
allfriendsmarks =
45 65 57 86 49 56 99 87 67 60
But when I use a colon, allfriendsmarks=m(:) results in:
allfriendsmarks =
45
65
57
86
49
56
99
87
67
60
Why do both methods return a different result?
I am learning C programming and I'm currently on a classic one, the insertion sort algorithm.
For contextualization, I have a set of 3 arrays to test on :
the first one which is disordered
the second one is in a descending order
and the last one is is the ordered one but with a swap between 2 elements.
Here is my code :
int insertionSort(int* tab, int n){
int i;
for (i=1; i<n; i++){
int j = i;
while(tab[j] < tab[j-1]) {
swap(&tab[j-1], &tab[j]);
j-=1;
}
}
affiche(tab, n);
return 0;
}
For the 2 last arrays, it works fine.
But for the first one I got :
6 6 7 8 10 32521 14 15 17 19 20 21 23 25 26 28 28 28 32 32 34 35 38 38 39 43 44 46 48 49 50 58 59 62 64 65 69 71 75 79 79 79 81 84 86 89 92 93 97 99
instead of
3 6 6 7 8 10 14 15 17 19 20 21 23 25 26 28 28 28 32 32 34 35 38 38 39 43 44 46 48 49 50 58 59 62 64 65 69 71 75 79 79 79 81 84 86 89 92 93 97 99
As you can see, the algorithm works fine for a big part of the array, but for the sixth minimal values, I have random values (sometimes there are 92,93,97... for the first ones).
I don't have any core dump, which means that I am in the array's space in memory, but some values like 32521 let me think that my index j goes too far in memory.
Frankly, I don't see where the problem is.
Look at this loop
while(tab[j] < tab[j-1]) {
swap(&tab[j-1], &tab[j]);
j-=1;
}
j can run happily all over the place. There needs to be something that detects that j isnt < 0 (actually < 1 since you access 'tab[j-1]')
maybe
while((j >= 1) && (tab[j] < tab[j-1]) ) {
swap(&tab[j-1], &tab[j]);
j-=1;
}
So I'm trying to write some pretty basic code to essentially create an array of random numbers, based on certain rules. My end goal is to try to have an array of numbers, in which none of them match one another. However, it seems like the end array that my code is outputting has numbers that match, and I can't seem to figure out why.
I've pasted a sample output below, and as you can see, some of the numbers in the 'Totals' array match. I'm guessing something is wrong with the way I wrote the recursive 'addboard' function, but I have no clue what's wrong. If anyone could provide some advice, that'd be great. Thanks.
function [Components,Totals] = BoardForm1()
BoardLengths = [4,6,8,10,12];
Initial = [0,1,2,3,4,5,6,7];
Components = zeros(8,14);
Totals = zeros(8,14);
for i=1:14
for row = 1:length(Initial)
[currentboard,test] = addboard(row,BoardLengths,Initial,Totals);
Initial(row) = test;
Components(row,i) = currentboard
Totals(row,i) = test
end
end
end
function [currentboard,test] = addboard(x,BoardLengths,Initial,Totals)
currentboard = BoardLengths(randi(length(BoardLengths)));
test = Initial(x) + currentboard;
if ismember(test,Totals)
addboard(x,BoardLengths,Initial,Totals);
end
end
Totals =
12 16 28 34 44 56 68 76 84 94 106 114 118 128
13 25 35 39 43 49 53 61 65 75 83 91 95 103
6 18 22 34 42 50 54 66 72 82 86 92 104 112
15 23 35 41 51 57 63 69 73 81 87 99 105 111
14 26 36 48 58 68 80 90 100 104 108 114 120 130
9 13 23 27 31 37 43 49 55 61 65 75 87 91
12 24 34 42 46 54 60 64 72 76 82 88 92 96
19 29 33 39 47 57 69 77 83 89 101 109 119 125
MATLAB passes by value, so any changes made in the recursed addboard is ignored since its output values are ignored. Fix by setting the output values of [currentboard, test] = addboard
In general, I recommend doing this iteratively (while loop) instead of recursively. There may even be a one-liner that can do this, but I'm not sure from the comments what the requirements of the board are.
I am unable to understand your code, maybe some comments what the functions are supposed to do would be helpful, but just on a formal level reading the code there is at least one error in these lines:
if ismember(test,Totals)
addboard(x,BoardLengths,Initial,Totals);
end
You are calling addboard without output arguments which has no effect. It should probably be:
if ismember(test,Totals)
[currentboard,test] = addboard(x,BoardLengths,Initial,Totals);
end
I am trying to add a function to a software (not written by me, no sources available) in which a byte string seems to be convertet somehow:
I am reading an ID from a RFID token: as Bytes:
52 55 48 48 69 54 52 65 54 65 this ID is written in the database as 57770346.
Not just the entries are different, the length is, too. Maybe the first entrys are cut off.
How could the function look like? How could i find it?
Here is some additional data:
52 55 48 48 69 54 52 65 54 65 -> 57770346
52 55 48 48 69 53 68 67 51 49 -> 57742129
48 49 48 52 68 70 57 69 65 68 -> 76731309
Found it myself:
BIN to ASCII
ASCII as HEX to INT
(first 4 characters are missing in the DB)
I'm embarrassed...
I have an 1D matrix in MATLAB that in created with best answer of mentioned question below, I would like to reorder it to first array:
Matrix "Zigzag" Reordering
Source: Wikidpedia: Jpeg
m = [69 9 75 46 23 16 100 83 92 54 8 45];
zigzag_writing(m, 4, 3)
ans=
69 9 16 100
75 23 83 8
46 92 54 45
You can find the invzigzag at matlab file exchange.
>> invzigzag(m,3,4)
ans =
69 9 16 100
75 23 83 8
46 92 54 45