how to control the length of indexed array element?

2 visualizaciones (últimos 30 días)
Mohamed Salah
Mohamed Salah el 10 de Mayo de 2020
Editada: per isakson el 11 de Mayo de 2020
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 comentarios
Tommy
Tommy el 10 de Mayo de 2020
Can you explain a bit more? What should m_xx equal after each iteration?
Mohamed Salah
Mohamed Salah el 10 de Mayo de 2020
Editada: Mohamed Salah el 10 de Mayo de 2020
m_xx should be equal to m(i) (from 1 to the last bit of m(i)) multiplied by factor m_d(1)

Iniciar sesión para comentar.

Respuesta aceptada

per isakson
per isakson el 10 de Mayo de 2020
Editada: per isakson el 11 de Mayo de 2020
"(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 comentarios
Tommy
Tommy el 11 de Mayo de 2020
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 el 11 de Mayo de 2020
I added to my answer

Iniciar sesión para comentar.

Más respuestas (1)

Mehmed Saad
Mehmed Saad el 10 de Mayo de 2020
Editada: Mehmed Saad el 10 de Mayo de 2020
  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 comentarios
Mohamed Salah
Mohamed Salah el 11 de Mayo de 2020
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 el 11 de Mayo de 2020
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

Iniciar sesión para comentar.

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by