I have a matrix a
from a I have generated a new matrix b which have 2 new elements for 1 element of a
a = [2;3]
[m,n]=size(a)
for i=1:m
b=[1.5*(a(i)) 2*(a(i))]
end
I want to generate a new matrix 'c' such that it includes all the sets
expected results as
c = [2 3;2 4;3 4.5;3 6]

댓글 수: 2

KSSV
KSSV 2021년 3월 1일
What is C? How you got it?
Karanvir singh Sohal
Karanvir singh Sohal 2021년 3월 1일
편집: Karanvir singh Sohal 2021년 3월 1일
I want to generate c matrix. For a(1,1) i have 2 values in b i.e. b=[1.5*2 2*2] Which gives b=[3 4] And similarly for a(2,1) I'll have b=[4.5 6]
I want new matrix c
C=[a(1) 1.5*a(1); a(1) 2*a(1); a(2) 1.5*a(2); a(2) 2*a(2)]

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

 채택된 답변

Karanvir singh Sohal
Karanvir singh Sohal 2021년 3월 3일

0 개 추천

Well I got what i was trying to do :)
Thanks @Jan for your time and help
L=4
X_Range=[L*1000/16 L*1000/10];
Shut= [152.4; 228.6; 304.8; 381.0; 457.2; 533.4; 609.6; 685.8; 762.0; 838.2; 914.4; 990.6; 1066.8];
X = Shut(Shut>=X_Range(1,1) & Shut<= X_Range(1,2));
[m,n]=size(X);
index = 0;
for i=1:m
Y_Range =[1.5*X(i) 2*X(i)];
Y = Shut(Shut>=Y_Range(1,1) & Shut<= Y_Range(1,2));
[mY,nY]=size(Y);
for j=1:mY
index=index+1;
XD(index)=X(i);
YD(index)=Y(j);
end
end
D= [reshape(XD,[],1) [reshape(YD,[],1)]]

댓글 수: 3

If this works, here is a simplified version:
L = 4;
X_Range = [L*1000/16 L*1000/10];
Shut = [152.4; 228.6; 304.8; 381.0; 457.2; 533.4; 609.6; ...
685.8; 762.0; 838.2; 914.4; 990.6; 1066.8];
X = Shut(Shut>=X_Range(1,1) & Shut<= X_Range(1,2));
XD = [];
YD = [];
for i = 1:numel(X)
Y = Shut(Shut >= 1.5*X(i) & Shut <= 2*X(i));
mY = numel(Y);
XD(end + 1:end + mY) = X(i);
YD(end + 1:end + mY) = Y;
end
D = [XD(:), YD(:)];
Thanks this is a really simple version.
can you please explain me this part:
XD(end + 1:end + mY) = X(i);
YD(end + 1:end + mY) = Y;
These lines do the same as your loop:
for j=1:mY
index=index+1;
XD(index)=X(i);
YD(index)=Y(j);
end
but more efficiently.

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

추가 답변 (1개)

Jan
Jan 2021년 3월 1일

0 개 추천

a = [2; 3];
b = [1.5; 2];
c = kron(a, [ones(size(a,1), 1), b])

댓글 수: 5

Karanvir singh Sohal
Karanvir singh Sohal 2021년 3월 1일
This won't work if i have more number of rows in a matrix as compared to b matrix
Jan
Jan 2021년 3월 1일
편집: Jan 2021년 3월 2일
My code does solve the problem, which you have asked for in your question.
It would be useful, if you explain, what the actual problem is. Posting a specific problem, when you want to solve a general one wastes the time of the persons, who want to help you to solve your problem.
"Wont work" does not allow to understand, what you want to get as result instead.
Maybe you want:
c = kron(a, [ones(size(a)), b])
% or
c = [a, a(:) * b(:).']
I do understand that I need to post general problem rather than be specific. Sorry for that
Here I'm attaching my code
L=4
X_Range=[L*1000/16 L*1000/10];
Shut= [152.4; 228.6; 304.8; 381.0; 457.2; 533.4; 609.6; 685.8; 762.0; 838.2; 914.4; 990.6; 1066.8]
X = Shut(Shut>=X_Range(1,1) & Shut<= X_Range(1,2))
[m,n]=size(X)
Shut(:,1);
for i=1:m
Y_Range =[1.5*X(i) 2*X(i)];
Y = Shut(Shut>=Y_Range(1,1) & Shut<= Y_Range(1,2));
c = kron(X, [ones(size(X,1), 1), Y])
endfor
Desired output:
c = [304.80 533.40; 304.80 609.60; 381.00 609.60; 381.00 685.80; 381.00 762.00]
When I used
c = kron(X, [ones(size(X,1), 1), Y])
elements of 2nd column are not as per required(Numerical value changed).
While using
c = kron(XDim, [ones(size(XDim), YDim)])
following erorr is recived
error: conversion of 533.4 to int64_t value failed
if we use
c = [XDim, XDim(:) * YDim(:).']
error: horizontal dimensions mismatch (2x1 vs 3x1)
and also values of Y in c matrix are changed.
I used for loop to obtain the desired results
L=4
X_Range=[L*1000/16 L*1000/10];
Shut= [152.4; 228.6; 304.8; 381.0; 457.2; 533.4; 609.6; 685.8; 762.0; 838.2; 914.4; 990.6; 1066.8];
X = Shut(Shut>=X_Range(1,1) & Shut<= X_Range(1,2));
[m,n]=size(X);
Shut(:,1);
for i=1:m
Y_Range =[1.5*X(i) 2*X(i)];
Y = Shut(Shut>=Y_Range(1,1) & Shut<= Y_Range(1,2));
[mY,nY]=size(Y);
for j=1:mY
c(j,1)=X(i);
endfor
c(:,2)=Y
endfor
This gives me the correct matrix but, I want to combine each c matrix.
when loop runs for first time it gives me c matrix of 2x2 and on second run i have 3x2
I want to combine c matrixs to get 5x2 in above example
Jan
Jan 2021년 3월 2일
I do not get an idea of what you want to achieve.

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

카테고리

도움말 센터File Exchange에서 GPU Computing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by