append new row in loop

조회 수: 2 (최근 30일)
Khairul Nur
Khairul Nur 2019년 10월 28일
편집: Khairul Nur 2019년 10월 31일
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

답변 (1개)

Bhaskar R
Bhaskar R 2019년 10월 28일
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
Khairul Nur
Khairul Nur 2019년 10월 31일
편집: Khairul Nur 2019년 10월 31일
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

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by