Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
converting 1D arrays one row to columns

Subject: converting 1D arrays one row to columns

From: talal hammouri

Date: 22 Dec, 2010 14:07:05

Message: 1 of 10

 hi all,

 suppose we have 1D array of M 1x9
 how to convert them to a 1D M 9x1
 (i.e M 9x1 TO M 1x9)
     

  M 1x9 =[1 2 3 4 5 6 7 8 9]
  M 9x1 =[1
               2
               3
               4
               5
               6
               7
               8
               9]

 Best regards;


 Talal

Subject: converting 1D arrays one row to columns

From: Nasser M. Abbasi

Date: 22 Dec, 2010 14:09:07

Message: 2 of 10

On 12/22/2010 6:07 AM, talal hammouri wrote:
> hi all,
>
> suppose we have 1D array of M 1x9
> how to convert them to a 1D M 9x1
> (i.e M 9x1 TO M 1x9)
>
>
> M 1x9 =[1 2 3 4 5 6 7 8 9]
> M 9x1 =[1
> 2
> 3
> 4
> 5
> 6
> 7
> 8
> 9]
>
> Best regards;
>
>
> Talal


one way is to transpose it.

--Nasser

Subject: converting 1D arrays one row to columns

From: Wayne King

Date: 22 Dec, 2010 14:19:05

Message: 3 of 10

"talal hammouri" <talalah@yahoo.com> wrote in message <iet0m9$11j$1@fred.mathworks.com>...
> hi all,
>
> suppose we have 1D array of M 1x9
> how to convert them to a 1D M 9x1
> (i.e M 9x1 TO M 1x9)
>
>
> M 1x9 =[1 2 3 4 5 6 7 8 9]
> M 9x1 =[1
> 2
> 3
> 4
> 5
> 6
> 7
> 8
> 9]
>
> Best regards;
>
>
> Talal

As Nassar correctly states, you can transpose it, but be careful if your vector contains complex entries. Use the .' operator to transpose without taking the complex conjugate.

x = randn(1,9)+1j*randn(1,9);
y = x.'; % does not take the conjugate

You can also do:
y = x(:); % this wil not conjugate complex-valued elements

Wayne

Subject: converting 1D arrays one row to columns

From: talal hammouri

Date: 22 Dec, 2010 14:39:05

Message: 4 of 10

  Dear Naser,

 Thank you

   Talal

Subject: converting 1D arrays one row to columns

From: Jan Simon

Date: 22 Dec, 2010 15:14:07

Message: 5 of 10

Dear talal,

Some methods:

> suppose we have 1D array of M 1x9
> how to convert them to a 1D M 9x1
> M = [1 2 3 4 5 6 7 8 9]

T1 = transpose(M); % or ctranspose
T2 = M(:); % Fast
T3 = reshape(M, numel(M), 1);
T4 = reshape(M, [], 1); % Fast
T4 = permute(M, [2, 1]);

Jan

Subject: converting 1D arrays one row to columns

From: dpb

Date: 22 Dec, 2010 15:27:11

Message: 6 of 10

Jan Simon wrote:
> Dear talal,
>
> Some methods:
>
>> suppose we have 1D array of M 1x9 how to convert them to a
>> 1D M 9x1 M = [1 2 3 4 5 6 7 8 9]
>
> T1 = transpose(M); % or ctranspose
> T2 = M(:); % Fast
> T3 = reshape(M, numel(M), 1);
> T4 = reshape(M, [], 1); % Fast
> T4 = permute(M, [2, 1]);

at least one more (and the one I reach for instinctively)--

T5 = M';

--

Subject: converting 1D arrays one row to columns

From: talal hammouri

Date: 22 Dec, 2010 15:40:28

Message: 7 of 10

 Dear Jan,

 really your answer is so fast. T2=m(:)
 hope you will help in other issue please.

 Thank you so much

 Talal

Subject: converting 1D arrays one row to columns

From: Sean de

Date: 22 Dec, 2010 15:50:22

Message: 8 of 10

"Jan Simon" wrote in message <iet4jv$ini$1@fred.mathworks.com>...

> T3 = reshape(M, numel(M), 1);
> T4 = reshape(M, [], 1); % Fast

Jan, my computer (MAC 64bit r2009b) disagrees with you.
%%%
function time_test_reshape

M = ones(1000000,1);
t1 = 0;
t2 = 0;
for ii = 1:100
    tic
    T3 = reshape(M, numel(M), 1);
    t1 = t1+toc;
    tic
    T4 = reshape(M, [], 1);
    t2 = t2+toc;
end

t1
t2
%%%

%{
t1 = 0.00017938
t2 = 0.00074196
%}

Which this has always amazed me. I'd think that since it's so much faster to use numel(), reshape() would do this internally for a requested 1-dimensional vector output case.

Subject: converting 1D arrays one row to columns

From: Jan Simon

Date: 22 Dec, 2010 17:19:08

Message: 9 of 10

Dear Sean de,

> Jan, my computer (MAC 64bit r2009b) disagrees with you.

The JIT acceleration can results in not realistic timings for not realistic tests.

 M = rand(1, 1e5); % I've transposed this...
 t_numel = 0;
 t_empty = 0;
 for ii = 1:10000
     tic
     T3 = reshape(M, numel(M), 1);
     numel = numel +toc;
     tic
     T4 = reshape(M, [], 1);
     t_empty = t_empty+toc;
 end

On my PC 32 bit, WinXP, PentiumM single core:
  t_numel = 0.031
  t_empty = 0.017
So this is equivalent to your results.

But if I insert some methods to confuse the JIT-accelerator, the situation changes --- but in Matlab 2009a only gradually:
  clear('T3', 'T4');
or
  T3(1) = rand; % Causes a deep data copy of M
  T4(1) = rand; % Causes a deep data copy of M
Then t_numel is just some percent faster, but at least it is and I can confirm your observation for my system also.

If you go bacj to Matlab 6.5, the [] method was some percent faster than NUMEL, as one can expected considering the overhead of calling a function.
Anyhow, both methods are very fast and differ just by some percent for realistic tests. And the times for computations do not depend on the size of the input as long as no deep data copy is needed.
And M(:) is faster at all - most of all it is nicer!

Kind regards, Jan

Subject: converting 1D arrays one row to columns

From: talal hammouri

Date: 23 Dec, 2010 08:28:04

Message: 10 of 10


 Dear Jan,

 Thank you so much for your help.

 Related to the above subject i have faced a problem as described below :
  1- we read 10 framers stored in I1,...I10
  2- we calculate the differences between the succesive frames stored in 1D ARRAY AS histdiff1.
  3- remove the lowest 30 % of the frame differencing histdiff1 .

 Now my problem concerns with how to link the actual frame it self with the frame difference that we remove.

 IN SUMMARY HOW TO REMOVE THE ACTUAL FRAME RELATED TO THE SPECIFIC FRAME DIFFERENCE OF THAT REMOVED (i.e where to put the accompanying frames I1,...I10 ).

 how can we modify the code to do that ?
 Hope that i explain my problem well.
 may i will explain in example if you need ?

 THE CODE I HAVE WRITTEN WITH YOUR HELP.


  best regards, talal


% Calculate difference:
clear all;
close all;
clc;
Dir = 'C:\Documents and Settings\alhammouri.ta\Desktop\nom10\*.jpg';
Imgs = dir(Dir);
disp('the number of frames for differencing');
display(Imgs);
histdiff1=zeros(length(Imgs)-1,1);
sumhist=zeros(length(Imgs)-1,1);
data1=zeros(length(Imgs)-1,1);
I3 = imread(Imgs(1).name);
D=size(I3);
disp(['the image size of I: ',num2str(D)]);
# CALCULATE THE FRAME DIFFERENCING
for j=1:length(Imgs)-1
    I1 = imread(Imgs(j).name);
    I2 = imread(Imgs(j+1).name);
    Y1=double(I1);
Y2=double(I2);
histdiff=(abs(hist(Y1)-hist(Y2)));
threshhist=histdiff > 4;
sumhist(j)=sum(sum(threshhist));
title(' The total image histogram difference')
title('The second Image Histogram')
disp([j,j+1]);
disp (['The total histogram difference :',num2str(sumhist(j))]);
histdiff1(j)=sumhist(j);
toc;
end
display('1st frame 2nd frame Total difference');
d1=0;
clc;
 % JUST FOR PRINTING
for j=1:length(Imgs)-1
    disp([j,j+1,histdiff1(j)]);
        d1=d1+histdiff1(j);
    end
disp (['The avg histogram difference :',num2str(avg)]);
toc;
display(Imgs);
m=length(Imgs)-1;
z=(1:m);
z1=z(:);
data1=[z1,histdiff1];
 [Y,I] = sort(data1(:,2));
 thirtypercent = round(0.7*length(Y));
 removeindices = I(1:thirtypercent);
 data1(removeindices,:) = [];
 display(data1);

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us