how to control the length of indexed array element?

조회 수: 6 (최근 30일)
Mohamed Salah
Mohamed Salah 2020년 5월 10일
편집: per isakson 2020년 5월 11일
m=[1 2 3 4];
m_d=0.5.*ones(1,length(m));
for i=1:length(m)
m_xx=m(i)(1:(length(m(i))*m_d(1))); %error is here
end
I want to control an indexed array element length for my project and I really can't get the correct syntax for this
  댓글 수: 2
Tommy
Tommy 2020년 5월 10일
Can you explain a bit more? What should m_xx equal after each iteration?
Mohamed Salah
Mohamed Salah 2020년 5월 10일
편집: Mohamed Salah 2020년 5월 10일
m_xx should be equal to m(i) (from 1 to the last bit of m(i)) multiplied by factor m_d(1)

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

채택된 답변

per isakson
per isakson 2020년 5월 10일
편집: per isakson 2020년 5월 11일
"(from 1 to the last bit of m(i)) "
Here is my shot in the dark
%%
m = uint8([1,2,3,4]);
len = length(m);
m_d = 0.5*ones(1,len);
m_xx = cell(1,len);
for jj = 1 : len
m_xx{jj} = bitget( m(jj), [1:8], 'uint8' );
end
%%
m_xx{1}
m_xx{4}
outputs
ans =
1×8 uint8 row vector
1 0 0 0 0 0 0 0
>> m_xx{4}
ans =
1×8 uint8 row vector
0 0 1 0 0 0 0 0
Comments
  • bitget only takes whole numbers (I chose uint8 to keep the output short)
  • cannot [not meaningsful to] multiply an integer with 0.5
  • I stored the result in a cell array, but there are other alternatives
In reponse to comment
Now I understand your question, I think.
Mehmed Saad have listed problems with your puzzling statement.
Your statement
m_xx = a(1:(length(a)*.5));
works because the length(a) is an even number.
Try
%%
a=[1 2 3 4];
b=[3 4 5 6];
c=[1 2 4 5];
m=[a b c]; % concatenates a, b and c into a row of length 12. That's less useful.
%%
m = { a, b, c }; % cell array. Comma is more readable than space
len = length( m );
m_xx = cell( 1, len ); % pre-allocate cell array for holding the results
for jj = 1 : length( m ) % i is the imaginary unit
m_xx{jj} = m{jj}( 1 : length(m{jj})*0.5 );
end
and
>> m_xx{:}
ans =
1 2
ans =
3 4
ans =
1 2
Comment
The statement in the for-loop looks a lot like your statement. However, you missed
  • that m and m_xx need to be cell arrays and
  • the difference between () and {} when using cell arrays.
  댓글 수: 3
Tommy
Tommy 2020년 5월 11일
Ah. See if this gets you where you're going:
a=[1 2 3 4];
b=[3 4 5 6];
c=[1 2 4 5];
m={a;b;c}; % a, b, and c can be different lengths
for i=1:numel(m)
m_xx=m{i}(1:end/2)
end
per isakson
per isakson 2020년 5월 11일
I added to my answer

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

추가 답변 (1개)

Mehmed Saad
Mehmed Saad 2020년 5월 10일
편집: Mehmed Saad 2020년 5월 10일
  1. you cannot feed 0 as array index it will give error
  2. for loop runs from 0 to length of m which will make it run for 5 iterations instead of 4
  3. m(i) will give you ith index. 1:length(m(i)) will be 1 as length(m(i)) will always be 1
  4. in order to access m's index from 1 to i, you have to replace all that code with m(1:i). also start for loop from 1 and not from 0
  댓글 수: 5
Mohamed Salah
Mohamed Salah 2020년 5월 11일
length of m in my code is 4000 so that's not the problem
in your last element of the the list you said no operator so the error appears so that's exactly my question I don't know the operator to make m_xx half the length of m(1)
Mohamed Salah
Mohamed Salah 2020년 5월 11일
that's the comment I wrote on the answer below that maybe help to clarify things
look suppose that
a=[1 2 3 4];
b=[3 4 5 6];
c=[1 2 4 5];
m=[a b c];
So I want to get m(1) half length -for example-
so I'd write
m_xx=m(1)(1:(length(m(1))*0.5) %and here comes the error
an working example without the for loop I want to make is to take every element of m directly like this
m_xx=a(1:(length(a)*.5));
i want m_xx=[1 2] from the first half of a

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

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by