this is my code where it has a matrix where it adds each row with its col where
as example
sum of row 1 =4
sum of col 1= 10
my number will be 14
so far my code works correct where number calculates all rows with col and gives this display
number =
14 18 22 26
in my loop os where i went wrong it split it with a counter to show like
number 1 has 14
number 2 has 18
number 3 has 22
number 4 has 26
it works for the first one and goes to an infinite loop and even not displaying the rest of the numbers can you tell me how to work with this and where did i go wrong thank you
first loop result
matrix =
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
1 has 14 number
12 has 22 number
code
matrix=[1 2 3 4;1 2 3 4;1 2 3 4;1 2 3 4]
number= sum(matrix)+sum(matrix');
number
len= length(number);
x=1;
y=1;
number(1,y) ; %
while x<=len
fprintf('%x has %d number \n',x,number)
x+1;
y+1;
number
end
Desired output:
matrix =
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
number =
14 18 22 26
1 has 14
2 has 18
3 has 22
4 has 26
There are multiple errors in your loop:
y is unused, you can delete it.
numbers prints the full array each iteration, not what you want.
x+1 calculates x+1 but does nothing with the result. Use x=x+1; instead.
When using fprint, you are using a the full array number instead of the right number number(x) as an input argument.
matrix=[1 2 3 4;1 2 3 4;1 2 3 4;1 2 3 4]
number= sum(matrix)+sum(matrix');
number
len= length(number);
x=1;
while x<=len
fprintf('%x has %d number \n',x,number(x))
x=x+1;
end
matrix=[1 2 3 4;1 2 3 4;1 2 3 4;1 2 3 4]
number= sum(matrix)+sum(matrix');
number
len= length(number);
x=1;
while x<=len
fprintf('%x has %d number \n',x,number(x))
x=x+1;
end
Related
Let's assume that we have a table with two columns. The table contains data and our goal is to sort that table.
Assume our data looks like this, where y1 and y2 are the data in the columns.
You can produce that plot with MATLAB or GNU Octave.
% Simulate the model
[t,y] = ode45(#odefunc,[0 20],[1; -2]);
% Plot the simulation
close all
plot(t,y(:,1),'-r',t,y(:,2),'-b')
title('Solution of van der Pol Equation (\mu = 1) with ODE45');
xlabel('Time t');
ylabel('Solution y');
legend('y_1','y_2')
grid on
function dydt = odefunc(t,y)
dydt = [y(2); (1-0.1*y(1)^2)*y(2)-y(1) + 1];
end
If we look above the plot, we are going to se the data like this:
You can create that plot with this code:
% Plot 3D bar
figure
imagesc(y)
colorbar
Here we can see that the plot have a very much like a "table-look". My question is what algorithm is used when sorting the rows in the table so every row that looks almost the same, have it's own unique position in the table.
For example, if we have a table like this.
0 2 4
1 3 5
2 4 6
3 5 7
4 6 8
5 7 9
0 2 4
1 3 5
2 4 6
3 5 7
4 6 8
5 7 9
0 2 4
1 3 5
2 4 6
3 5 7
4 6 8
5 7 9
0 2 4
1 3 5
The code if you want to create that table.
j = 0;
rows = 20;
for i = 1:rows
disp(sprintf("%i %i %i", j, j+2, j+4))
j = j + 1;
if(j + 4 >= 10)
j = 0;
end
end
We can see that there are four rows of 0 2 4 and three rows of 5 7 9.
I want all rows 0 2 4 close to each other and all rows 5 7 9 close to each other. And.... 0 2 4 cannot be after 5 7 9 because then the plot would look terrible.
For example, assume that we begining with row 1, the first row 0 2 4. Then we are looking for the same rows of 0 2 4 and let's say we found four rows 0 2 4. Then we sort them.
0 2 4
0 2 4
0 2 4
0 2 4
Now next row would be 1 3 5 and we find two rows of 1 3 5. We sorting them.
0 2 4
0 2 4
0 2 4
0 2 4
1 3 5
1 3 5
After we have sorted for a while, we are going to have a table like this.
0 2 4
0 2 4
0 2 4
0 2 4
1 3 5
1 3 5
2 4 6
2 4 6
2 4 6
2 4 6
3 5 7
3 5 7
3 5 7
.
.
.
.
5 7 9
5 7 9
5 7 9
And now, we found 1 2 4, which is very similar to 0 2 4. So we need to place 1 2 4 close to 0 2 4, perhaps between 0 2 4 or 1 3 5 or after 0 2 4 or before 0 2 4. How do I even know that 1 2 4 should be placed close to 0 2 4? That's the issue!!!.
How can I sort that?
I need to do that in C-programming language because speed is most important here, but I think I will start to do it in GNU Octave. I'm pretty sure that there is a SQL-sorting algorithm I'm looking for.
Notice in practice, there are numbers, integers, 10-bit e.g values between 0-1023.
I am trying to implement a stem and plot algorithm in MATLAB for educational purposes. Before I post my code, let me introduce what the steps of my approach are. Let us consider that we have two digit numbers:
A=[20 12 13 21 56 13 16 17 22 23 24];
Stems can be given by
stems=fix(A/10)
stems =
2 1 1 2 5 1 1 1 2 2 2
and leafs can be given by
leaf=fix(mod(A,10))
leaf =
0 2 3 1 6 3 6 7 2 3 4
What I have done, is to sort the stems and according to that sort leafs as well:
[stems, index]=sort(stems,'ascend')
leaf=leaf(index)
stems =
1 1 1 1 1 2 2 2 2 2 5
leaf =
2 3 3 6 7 0 1 2 3 4 6
This is the basic idea:
Count the frequency of occurrence of each number in stems
Take that many elements from leaf
Repeat this procedure for each stem, where at each step I am shortening the leaf array. So for instance for stems = 1, we have [5 1], so I will have
leaf(1:5)
ans =
2 3 3 6 7
leaf(1:5)=[]
leaf =
0 1 2 3 4 6
stems = 2 is again 5 times, so again:
leaf(1:5)
ans =
0 1 2 3 4
leaf(1:5)=[]
leaf =
6
Now for stems = 5, we have 1 leaf
leaf(1)
ans =
6
For this I used a map container and I have created the following code:
function stem_leaf_plot(v)
if ~isnumeric(v) % check that program will accept array as a integers
error( 'Input V must be numeric');
end
stems=fix(v/10);
leaf=fix(rem(v,10));
[stems, index]=sort(stems,'ascend');
leaf=leaf(index);
string_stems=num2str(stems);
%% count occurence of each stem
MAP=containers.Map();
n=length(stems); % total element of stems array
for ii=1:n
if isKey(MAP,string_stems(ii))
MAP(string_stems(ii))= MAP(string_stems(ii))+1;
else
MAP(string_stems(ii))=1;
end
end
MAP_count=length(MAP);
stem=num2str(cell2mat(keys(MAP)));
for jj=1:MAP_count
frequency=(MAP(string_stems(jj)));
fprintf('leafs of stem %d',stem(jj));
disp(leaf(1:frequency));
leaf(1:frequency)=[]; % delete elements step by step
end
end
However, the result of my code is
stem_leaf_plot(A)
leafs of stem 32 2 3 3 6
leafs of stem 49 7 0 1 2 3 4 6
What is wrong?
After suggestion of #Adriaan I used hist to count frequencies, instead of a container. Here is my updated code:
function stem_leaf_plot(v)
if ~isnumeric(v) % check that program will accept array as a integers
error( 'Input V must be numeric');
end
stems=fix(v/10);
leaf=fix(rem(v,10));
[stems, index]=sort(stems,'ascend');
leaf=leaf(index);
[a,b]=hist(stems,unique(stems));
n=length(a);
for ii=1:n
fprintf('leaf of stem %d is ',b(ii));
leaf(1:a(ii))
leaf(1:a(ii))=[];
end
>> A=[20 12 13 21 56 13 16 17 22 23 24];
>> stem_leaf_plot(A)
leaf of stem 1 is
ans =
2 3 3 6 7
leaf of stem 2 is
ans =
0 1 2 3 4
leaf of stem 5 is
ans =
6
I need some help please. I have an array, as shown below, 6 rows and 5 columns, none of the elements in any one row repeats. The elements are all single digit numbers.
I want to find out, per row, when a number, let's say 1 appears, I want to keep of how often the other numbers of the row appear. For example, 1 shows up 3 times in rows one, three and five. When 1 shows up, 2 shows up one time, 3 shows up two times, 4 shows up two times, 5 shows up one time, 6 shows up two times, 7 shows up one time, 8 shows up three times, and 9 shows up zero times. I want to keep a vector of this information that will look like, V = [3,1,2,2,1,2,1,3,0], by starting with a vector like N = [1,2,3,4,5,6,7,8,9]
ARRAY =
1 5 8 2 6
2 3 4 6 7
3 1 8 7 4
6 5 7 9 4
1 4 3 8 6
5 7 8 9 6
The code I have below does not give the feedback I am looking for, can someone help please? Thanks
for i=1:length(ARRAY)
for j=1:length(N)
ARRAY(i,:)==j
V(j) = sum(j)
end
end
Using indices that is in A creae a zero and one 6 * 9 matrix that [i,j] th element of it is 1 if i th row of A contains j.
Then multiply the zero and one matrix with its transpose to get desirable result:
A =[...
1 5 8 2 6
2 3 4 6 7
3 1 8 7 4
6 5 7 9 4
1 4 3 8 6
5 7 8 9 6]
% create a matrix with the size of A that each row contains the row number
rowidx = repmat((1 : size(A,1)).' , 1 , size(A , 2))
% z_o a zero and one 6 * 9 matrix that [i,j] th element of it is 1 if i th row of A contains j
z_o = full(sparse(rowidx , A, 1))
% matrix multiplication with its transpose to create desirable result. each column relates to number N
out = z_o.' * z_o
Result: each column relates to N
3 1 2 2 1 2 1 3 0
1 2 1 1 1 2 1 1 0
2 1 3 3 0 2 2 2 0
2 1 3 4 1 3 3 2 1
1 1 0 1 3 3 2 2 2
2 2 2 3 3 5 3 3 2
1 1 2 3 2 3 4 2 2
3 1 2 2 2 3 2 4 1
0 0 0 1 2 2 2 1 2
I don't understand how you are approaching the problem with your sample code but here is something that should work. This uses find, any and accumarray and in each iteration for the loop it will return a V corresponding to the ith element in N
for i=1:length(N)
rowIdx = find(any(A == N(i),2)); % Find all the rows contain N(j)
A_red = A(rowIdx,:); % Get only those rows
V = [accumarray(A_red(:),1)]'; % Count occurrences of the 9 numbers
V(end+1:9) = 0; % If some numbers don't exist place zeros on their counts
end
i have this sample matrix in matlab i can get the sum of column each separate
matrix=[1 2 3 4;1 2 3 4;1 2 3 4;1 2 3 4]
sum(matrix)
result
matrix =
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
ans =
4 8 12 16
i want to have these colum as they are and get sum of each row by itself like result should be
rowsum=10 10 10 10
then add each row with column separate as a final result
1=14
2=18
3=22
4=26
You can add the sum you've already calculated and the sum of the transposed matrix to get the result you're looking for;
> sum(matrix)+sum(matrix')
ans =
14 18 22 26
More generally, to calculate the sum of each row directly just give the relevant dimension to sum to sum over the columns instead of rows:
>> sum(matrix,2)
ans =
10
10
10
10
We have p.e. i = 1:25 iterations.
Each iteration result is a 1xlength(N) cell array, where 0<=N<=25.
iteration 1: 4 5 9 10 20
iteration 2: 3 8 9 13 14 6
...
iteration 25: 1 2 3
We evaluate the results of all iterations to one matrix sorted according to frequency each value is repeated in descending order like this example:
Matrix=
Columns 1 through 13
16 22 19 25 2 5 8 14 17 21 3 12 13
6 5 4 4 3 3 3 3 3 3 2 2 2
Columns 14 through 23
18 20 1 6 7 9 10 11 15 23
2 2 1 1 1 1 1 1 1 1
Result explanation: Column 1: N == 16 is present in 6 iterations, column 2: N == 22 is present in 5 iterations etc.
If a number N isn't displayed (in that paradigm N == 4, N == 24) in any iteration, is not listed with frequency index of zero either.
I want to associate each iteration (i) to the first N it is displayed p.e. N == 9 to be present only in first iteration i = 1 and not in i = 2 too, N == 3 only to i = 2 and not in i = 25 too etc until all i's to be unique associated to N's.
Thank you in advance.
Here's a way that uses a feature of unique (i.e. that it returns the index to the first value) that was introduced in R2012a
%# make some sample data
iteration{1} = [1 2 4 6];
iteration{2} = [1 3 6];
iteration{3} = [1 2 3 4 5 6];
nIter= length(iteration);
%# create an index vector so we can associate N's with iterations
nn = cellfun(#numel,iteration);
idx = zeros(1,sum(nn));
idx([1,cumsum(nn(1:end-1))+1]) = 1;
idx = cumsum(idx); %# has 4 ones, 3 twos, 6 threes
%# create a vector of the same length as idx with all the N's
nVec = cat(2,iteration{:});
%# run `unique` on the vector to identify the first occurrence of each N
[~,firstIdx] = unique(nVec,'first');
%# create a "cleanIteration" array, where each N only appears once
cleanIter = accumarray(idx(firstIdx)',firstIdx',[nIter,1],#(x){sort(nVec(x))},{});
cleanIter =
[1x4 double]
[ 3]
[ 5]
>> cleanIter{1}
ans =
1 2 4 6
Here is another solution using accumarray. Explanations in the comments
% example data (from your question)
iteration{1} = [4 5 9 10 20 ];
iteration{2} = [3 8 9 13 14 6];
iteration{3} = [1 2 3];
niterations = length(iteration);
% create iteration numbers
% same as Jonas did in the first part of his code, but using a short loop
for i=1:niterations
idx{i} = i*ones(size(iteration{i}));
end
% count occurences of values from all iterations
% sort them in descending order
occurences = accumarray([iteration{:}]', 1);
[occ val] = sort(occurences, 1, 'descend');
% remove zero occurences and create the Matrix
nonzero = find(occ);
Matrix = [val(nonzero) occ(nonzero)]'
Matrix =
3 9 1 2 4 5 6 8 10 13 14 20
2 2 1 1 1 1 1 1 1 1 1 1
% find minimum iteration number for all occurences
% again, using accumarray with #min function
assoc = accumarray([iteration{:}]', [idx{:}]', [], #min);
nonzero = find(assoc);
result = [nonzero assoc(nonzero)]'
result =
1 2 3 4 5 6 8 9 10 13 14 20
3 3 2 1 1 2 2 1 1 2 2 1