append new row in loop

2 visualizaciones (últimos 30 días)
Khairul Nur
Khairul Nur el 28 de Oct. de 2019
Editada: Khairul Nur el 31 de Oct. de 2019
hi all, i need some solution. Please help. I want to create a function which has L is a matrix 1:11 and return a matrix 40:11 after 40 times of loop.
For example: everytime this function being call, it will carry L and append in row
1st call : L [13 25 15 11 11 14 12 12 10 14 1]
2nd call L [13 25 15 11 11 14 12 12 10 14 2]
....
end result, this function will return:
[13 25 15 11 11 14 12 12 10 14 1]
[13 25 15 11 11 14 12 12 10 14 2]
...
TQIA
function [] = sortmatrix(L)
% append row in matrix
for c = 1:40
b =(L(1,:))
b = L(b; %not sure how to add b into matrix
end
kk = reshape(b,[],11);
%disp(kk)
end

Respuestas (1)

Bhaskar R
Bhaskar R el 28 de Oct. de 2019
Its a simple concatanation operation, pass the values e as
L is your constant matrix and number iof loops
as
L = [13 25 15 11 11 14 12 12 10 14];
nLoops = 40;
Then execute your function
function result_mat = sortmatrix(L, nLoops)
% here nLoops is 40 times of loop and L is constant matrix
% E.g L = [13 25 15 11 11 14 12 12 10 14] and nLoops = 40
result_mat = zeros(nLoops, size(L,2)+1); % Initialize result matrix
for ii = 1:nLoops
result_mat(ii,:) = [L, ii]; % concatante loop value to result matrix
end
end
The variable result_mat is your required matrix
  1 comentario
Khairul Nur
Khairul Nur el 31 de Oct. de 2019
Editada: Khairul Nur el 31 de Oct. de 2019
the resulted matrix have the same value of totalk1 for 40 times which is wrong. I want the matrix to append updated value of totalk1.
here is my main:
for n = i:40
result1= sum((c1- MM(i,:)).^2)
result2= sum((c2- MM(i,:)).^2)
result3= sum((c3- MM(i,:)).^2)
result4= sum((c4- MM(i,:)).^2)
i=i+1
totalk1 = [result1,result2,result3,result4]
S=sort_euc(totalk1,40)
end
meanwhile sort_euc is the function as below:
function [sorted_euc] = sort_euc(totalk1,nLoops)
% here nLoops is 40 times of loop and L is constant matrix
% E.g L = [13 25 15 11 11 14 12 12 10 14] and nLoops = 40
sorted_euc = zeros(nLoops, size(totalk1,2)+1); % Initialize result matrix
for ii = 1:nLoops
sorted_euc(ii,:) = [totalk1,ii]; % concatante loop value to result matrix
end
end

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by